ボタンをクリックしたらボックスが表示←→非表示、とかいう動きを、とりあえず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の内部要素で上下のスペースを取る工夫あり。
横の拡縮とかも出来そうなので、そこは各自工夫で。
ピンバック: difference between sildenafil and tadalafil
ピンバック: viagra buy cheap
ピンバック: female viagra pill online
ピンバック: viagra voucher
ピンバック: generic viagra capsules
ピンバック: buy women viagra online
ピンバック: prices of cialis 20 mg
ピンバック: cost of cialis 20mg
ピンバック: soft cialis canadian pharmacy
ピンバック: generic cialis prices
ピンバック: metronidazole jaundice
ピンバック: gabapentin conjunctivitis
ピンバック: sulfamethoxazole trimethoprim and exercise
ピンバック: valacyclovir expensive
ピンバック: nolvadex stroke
ピンバック: does lyrica damage your liver
ピンバック: lasix rettungsdienst
ピンバック: metformin hypos
ピンバック: lisinopril monographie
ピンバック: semaglutide injection sites
ピンバック: rybelsus gastric emptying
ピンバック: rybelsus oral tablet
ピンバック: zoloft reddit
ピンバック: flagyl crying
ピンバック: side effects of duloxetine hcl dr 60 mg
ピンバック: does keflex work for uti
ピンバック: sertraline vs lexapro
ピンバック: viagra mail order
ピンバック: azithromycin kitten
ピンバック: can you take cephalexin for ear infection
ピンバック: escitalopram side effects
ピンバック: gabapentin gewöhnung
ピンバック: how to taper off of cymbalta
ピンバック: zoloft and fluoxetine
ピンバック: can you drink on ciprofloxacin
ピンバック: cephalexin medication
ピンバック: bactrim side effects rash
ピンバック: what is bactrim
ピンバック: amoxicillin during pregnancy
ピンバック: ezetimibe-related myopathy a systematic review
ピンバック: how long will i have to take flomax
ピンバック: diltiazem for afib dose
ピンバック: desmopressin ddavp stimate
ピンバック: what should you not take with citalopram
ピンバック: diclofenac sodium
ピンバック: cozaar coupon
ピンバック: what are the side effects of depakote
ピンバック: contrave drug class
ピンバック: effexor and wellbutrin combo reviews
ピンバック: does flexeril cause constipation
ピンバック: amitriptyline other names
ピンバック: low dose aspirin for dogs
ピンバック: probenecid versus allopurinol
ピンバック: brexpiprazole vs aripiprazole
ピンバック: side effects of celebrex 200mg
ピンバック: celexa max dosage
ピンバック: baclofen pump for cerebral palsy
ピンバック: augmentin 875 dosage
ピンバック: how does buspar make you feel
ピンバック: do i take ashwagandha in the morning or night
ピンバック: naltrexone 50 mg + bupropion sr 150 mg for weight loss reviews
ピンバック: protonix drug interactions
ピンバック: remeron vs wellbutrin
ピンバック: acarbose alternatives
ピンバック: actos-service
ピンバック: abilify vs rexulti
ピンバック: 3 months on semaglutide
ピンバック: define repaglinide
ピンバック: robaxin classification