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で自在にする」への46件のフィードバック

  1. ピンバック: tadalafil dosage for erectile dysfunction

  2. ピンバック: buy levitra generic

  3. ピンバック: teva vardenafil hydrochloride tablets

  4. ピンバック: target pharmacy flonase

  5. ピンバック: how long does it take for sildenafil to work

  6. ピンバック: sildenafil overdose

  7. ピンバック: sildenafil over the counter cvs

  8. ピンバック: tadalafil pulmonary hypertension

  9. ピンバック: priceline pharmacy xenical

  10. ピンバック: para que sirve sildenafil

  11. ピンバック: tadalafil 20 mg side effects

  12. ピンバック: levitra 10 mg buy online

  13. ピンバック: vardenafil hcl 20mg tab side effects

  14. ピンバック: sildenafil for pe

  15. ピンバック: cyclophosphamide online pharmacy

  16. ピンバック: erectile dysfunction drug

  17. ピンバック: vardenafil precio

  18. ピンバック: tadalafil 5 mg para que sirve

  19. ピンバック: is motrin 600 mg a narcotic

  20. ピンバック: gabapentin ilac

  21. ピンバック: can i take aspirin with sulfasalazine

  22. ピンバック: celebrex versus glucosamine

  23. ピンバック: carbamazepine ethosuximide

  24. ピンバック: how long can you take diclofenac?

  25. ピンバック: amitriptyline ssri

  26. ピンバック: imitrex not working for migraine

  27. ピンバック: taking elavil and xanax together

  28. ピンバック: miastenia gravis y mestinon

  29. ピンバック: rizatriptan benzoate wafer

  30. ピンバック: baclofen or diazepam

  31. ピンバック: maxalt liofilizzato orale

  32. ピンバック: quais os medicamentos que contem piroxicam

  33. ピンバック: meloxicam sandoz tablets

  34. ピンバック: sumatriptan grossesse

  35. ピンバック: lioresal sucht

  36. ピンバック: interactions with imuran

  37. ピンバック: best time to take azathioprine

  38. ピンバック: mobic solution injectable

  39. ピンバック: hydralazine and imdur

  40. ピンバック: cyproheptadine and ibuprofen

  41. ピンバック: tramadol and zanaflex interaction

  42. ピンバック: artane for blepharospasm

  43. ピンバック: tizanidine product information

  44. ピンバック: where can i get toradol online

  45. ピンバック: periactin ingrassare

  46. ピンバック: can i order cheap ketorolac without dr prescription

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です