
実現したいこと
ナビボタンの実装で、選択したらその部分のセクションまでスクロールさせたいです。
以下のバグが起こっています。
1 回目のボタン押下:全て正常動作(スクロールする。)
2 回目のボタン押下(list-section2 以外のボタンを 2 回押下):正常動作(スクロールする。)
2 回目のボタン押下(list-section2 を 1 度押下し他のボタンを押下):list-section2 が 1度正常動作(スクロール)をした後に list-section2 含めボタン押下しても反応しなくなる。
前提
-
1 回目動作時点では問題ないのですが、 list-section2 を一度押下したあとの動作は全て、 addeventlistener の onclick が効かなくなっています。
-
list-section2 を押した後に、list-section3 を押下した際に、list-section3 の中の console.log が表示されなくなります。
-
list-section2 を 2 回押下した際に、list-section2 のconsole.log が全て表示されません。
発生している問題・エラーメッセージ
エラーが表示されるようなことはありませんでした。
該当のソースコード
JavaScript
1$(window).on('load', function(){ 2 3 $('.list-section1').on('click', function() { 4 // 該当のポイントまで遷移させる 5 scrollTo(0, 50); 6 // 該当のナビの下線を付ける 7 $('li').css('border-bottom', 'white solid 2px'); 8 // 該当のナビの以降の下線を外す 9 $('li').each(function(idx, val) { 10 if(idx > 0) { 11 val.style.border = 'none'; 12 } 13 }); 14 15 console.log(window.pageYOffset); 16 17 }); 18 19 $('.list-section2').on('click', function() { 20 console.log('2-1'); 21 scrollTo(0, 750); 22 $('li').css('border-bottom', 'white solid 2px'); 23 $('li').each(function(idx, val) { 24 if(idx > 1) { 25 val.style.border = 'none'; 26 } 27 }); 28 console.log('2-2'); 29 30 console.log(window.pageYOffset); 31 console.log('2-3'); 32 33 }); 34 35 $('.list-section3').on('click', function() { 36 console.log('3-1'); 37 38 scrollTo(0, 1300); 39 console.log('3-2'); 40 41 $('li').css('border-bottom', 'white solid 2px'); 42 $('li').each(function(idx, val) { 43 if(idx > 2) { 44 val.style.border = 'none'; 45 } 46 }); 47 console.log('3-3'); 48 49 console.log(window.pageYOffset); 50 51 }); 52 53 $('.list-section4').on('click', function() { 54 scrollTo(0, 1960); 55 $('li').css('border-bottom', 'white solid 2px'); 56 $('li').each(function(idx, val) { 57 if(idx > 3) { 58 val.style.border = 'none'; 59 } 60 }); 61 62 console.log(window.pageYOffset); 63 64 }); 65 66 $('.list-section5').on('click', function() { 67 scrollTo(0, 2800); 68 $('li').css('border-bottom', 'white solid 2px'); 69 $('li').each(function(idx, val) { 70 if(idx > 4) { 71 val.style.border = 'none'; 72 } 73 }); 74 75 console.log(window.pageYOffset); 76 77 }); 78}); 79
html
1 <nav> 2 <ul> 3 <li class="list-section1">ホーム</li> 4 <li class="list-section2">自己紹介</li> 5 <li class="list-section3">経験言語</li> 6 <li class="list-section4">Works</li> 7 <li class="list-section5">Contact</li> 8 </ul> 9 </nav> 10
試したこと
$('.list-section2').on('click', function() {
}
の処理の中身全て(JavaScript 20 ~ 31 行目)をコメントアウトしたところ、list-section2 を押下後も、他のボタンは反応するようになりました。
回答2件
あなたの回答
tips
プレビュー