class付与をtoggleClassで自在にする

投稿者: | 2018年1月31日

ボタンをクリックしたらボックスが表示←→非表示、とかいう動きを、とりあえずclassを付けたらとりあえず何とかなる、という発想で解決した手法。

基本形。「.btnというclassが付いたボタンをクリックしたら、.openが付いてるdivに.activeというclassを追加する」という仕組み。

▼HTML



ボックス
▼JS




▼CSS

.open{display:none;}

.open.active{display:block;}

ポイントはCSSでopenはdisplay:noneで非表示にさせておいた上で、.activeが付いたらdisplay:blockにする点。
あと、classに向けて付与させるためのものか、反映させる要素にclass設定が入ってないとダメっぽい?(この場合.openのこと。#openにしてJSの指定を#openにしてもclass付与されない)

さらに、これに「.btnも色が変わる」とボタン自体にもクラスを付けた場合はこのようになる。





classの指定でもいいが、発動起点となるクリックした要素事態だからthisという指定でもOKらしい。その際は”は使わない。


    $(this).toggleClass('active');


これが「3つのボタン&3つのboxの開閉」となったらclassを変えて設置する。(HTMLとCSSの内容は書くのが面倒なので各自対応で)





ただ、上記の内容だとbtn01をクリックしbtn02をクリックすると、box1とbox2が同じ画面にあることになる。これはtoggleClassはclassを付け外しする命令のため、もう一度ボタンを押さないとクラスが外れないため。そこでクラスを付ける・外すだけの命令もある。

toggleClass・・・クリックするたびにクラスを付ける・外すを繰り返す
addClass・・・クラスを付ける
removeClass・・・クラスを外す

addClassでクラスを付けたらクラスはそのままになるので、クラスを消す処理が必要な場合はremoveClassで確実に外す処理が必要となる。
「画面には1つのboxだけが表示されて出し入れされる」というサンプルは以下の通りとなる。





めんどくさっ!
toggleClassではなくaddClassとremoveClassを組み合わせて、一つのボタンをクリックしたら他の要素からremoveClassでactiveを外す、という命令にすると対応可能、というわけ。
もっとスクリプトで高度なやり方がありそうだが、手持ちの武器でできる簡単な実装はこんな感じとなる。

ここまでは「クリックしたら」だったが、発動処理を「マウスオーバーしたら」にすることも可能。





あとおまけ。display:block、noneだとパッと開いちゃうので、これを少々アニメっぽく動く縦の拡縮にすることもできる。


.box{
 max-height: 0;
 overflow-y: hidden;
 -webkit-transition: all 0.2s;
 -moz-transition: all 0.2s;
 -ms-transition: all 0.2s;
 -o-transition: all 0.2s;
 transition: all 0.2s;
}

.box.active{
 max-height:1000px;
 -webkit-transition: all 0.8s;
 -moz-transition: all 0.8s;
 -ms-transition: all 0.8s;
 -o-transition: all 0.8s;
 transition: all 0.8s;
}


つまり、max-heightの高さに加えてtransitionでアニメっぽく見せている処理にしちゃうわけですな。
これも気を付けないといけないのは、.boxに上下のpaddingとか使っちゃうとそのスペース分だけ画面に残ってしまうので、.boxの内部要素で上下のスペースを取る工夫あり。
横の拡縮とかも出来そうなので、そこは各自工夫で。

class付与をtoggleClassで自在にする」への69件のフィードバック

  1. ピンバック: difference between sildenafil and tadalafil

  2. ピンバック: viagra buy cheap

  3. ピンバック: female viagra pill online

  4. ピンバック: viagra voucher

  5. ピンバック: generic viagra capsules

  6. ピンバック: buy women viagra online

  7. ピンバック: prices of cialis 20 mg

  8. ピンバック: cost of cialis 20mg

  9. ピンバック: soft cialis canadian pharmacy

  10. ピンバック: generic cialis prices

  11. ピンバック: metronidazole jaundice

  12. ピンバック: gabapentin conjunctivitis

  13. ピンバック: sulfamethoxazole trimethoprim and exercise

  14. ピンバック: valacyclovir expensive

  15. ピンバック: nolvadex stroke

  16. ピンバック: does lyrica damage your liver

  17. ピンバック: lasix rettungsdienst

  18. ピンバック: metformin hypos

  19. ピンバック: lisinopril monographie

  20. ピンバック: semaglutide injection sites

  21. ピンバック: rybelsus gastric emptying

  22. ピンバック: rybelsus oral tablet

  23. ピンバック: zoloft reddit

  24. ピンバック: flagyl crying

  25. ピンバック: side effects of duloxetine hcl dr 60 mg

  26. ピンバック: does keflex work for uti

  27. ピンバック: sertraline vs lexapro

  28. ピンバック: viagra mail order

  29. ピンバック: azithromycin kitten

  30. ピンバック: can you take cephalexin for ear infection

  31. ピンバック: escitalopram side effects

  32. ピンバック: gabapentin gewöhnung

  33. ピンバック: how to taper off of cymbalta

  34. ピンバック: zoloft and fluoxetine

  35. ピンバック: can you drink on ciprofloxacin

  36. ピンバック: cephalexin medication

  37. ピンバック: bactrim side effects rash

  38. ピンバック: what is bactrim

  39. ピンバック: amoxicillin during pregnancy

  40. ピンバック: ezetimibe-related myopathy a systematic review

  41. ピンバック: how long will i have to take flomax

  42. ピンバック: diltiazem for afib dose

  43. ピンバック: desmopressin ddavp stimate

  44. ピンバック: what should you not take with citalopram

  45. ピンバック: diclofenac sodium

  46. ピンバック: cozaar coupon

  47. ピンバック: what are the side effects of depakote

  48. ピンバック: contrave drug class

  49. ピンバック: effexor and wellbutrin combo reviews

  50. ピンバック: does flexeril cause constipation

  51. ピンバック: amitriptyline other names

  52. ピンバック: low dose aspirin for dogs

  53. ピンバック: probenecid versus allopurinol

  54. ピンバック: brexpiprazole vs aripiprazole

  55. ピンバック: side effects of celebrex 200mg

  56. ピンバック: celexa max dosage

  57. ピンバック: baclofen pump for cerebral palsy

  58. ピンバック: augmentin 875 dosage

  59. ピンバック: how does buspar make you feel

  60. ピンバック: do i take ashwagandha in the morning or night

  61. ピンバック: naltrexone 50 mg + bupropion sr 150 mg for weight loss reviews

  62. ピンバック: protonix drug interactions

  63. ピンバック: remeron vs wellbutrin

  64. ピンバック: acarbose alternatives

  65. ピンバック: actos-service

  66. ピンバック: abilify vs rexulti

  67. ピンバック: 3 months on semaglutide

  68. ピンバック: define repaglinide

  69. ピンバック: robaxin classification

コメントを残す

メールアドレスが公開されることはありません。