###実現したいこと
ヘッダーのボタンをクリックすると、
該当のアコーディオンパネルにスクロールし、開く。
###発生している問題
一回前にクリックしたハッシュが取得されてしまう。
###該当のソースコード
HTML
1<section id="btn"> 2 <div class="box_z"> 3 <a href="#a1">#a1</a> 4 </div> 5 <div class="box_z"> 6 <a href="#a2">#a2</a> 7 </div> 8 <div class="box_z"> 9 <a href="#a3">#a3</a> 10 </div> 11 </section> 12 <section id="acd"> 13 <div class="box_c" id="a1"> 14 <div class="box_a"> 15 #a1 16 </div> 17 <div class="box_b"> 18 </div> 19 </div> 20 <div class="box_c" id="a2"> 21 <div class="box_a"> 22 #a2 23 </div> 24 <div class="box_b"> 25 </div> 26 </div> 27 <div class="box_c" id="a3"> 28 <div class="box_a"> 29 #a3 30 </div> 31 <div class="box_b"> 32 </div> 33 </div> 34 </section>
jQuery
1$(function() { 2 // クリックしたらアコーディオン開閉 3 $(".box_a").click(function(){ 4 $this = $(this); 5 $this.next().stop(); 6 $this.next().slideToggle(); 7 }); 8 9 //ヘッダーナビのボタンを押すと、 10 //アンカーリンクしてアコーディオン開閉 11 $("#btn a[href^=#a]").click(function() { 12 //一旦全てのアコーディオン閉じる 13 $(".box_b").hide(); 14 //ハッシュ取得 15 var hash = $(location).attr("hash"); 16 console.log(hash); 17 //アンカーリンクのズレを補正 18 var headerHeight = 200; 19 var position = $(hash).offset().top - headerHeight; 20 console.log(position); 21 $("html, body").animate({scrollTop:position}, 550,"swing"); 22 //アコーディオン処理 23 $(hash).children(".box_b").stop(); 24 $(hash).children(".box_b").slideToggle(); 25 }); 26}); 27
どうぞ、よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/19 06:55
2017/03/19 10:06
2017/03/20 04:27