headerを全ページ共通で読み込み、下層ページからTOPページに遷移させた際にvar adjust = -100;
で位置調整をしたいのですが、そもそもjsの記述が発火していないように思われます。
TOPページ内ではjsが走るのですが、下層→TOPに移動した際にも動くようにするにはどうすれば良いでしょうか?
index.jsは遷移前/遷移後両方のページで読み込んでいます。
共通で読み込んでいるheader
header.pug
1ul.navList 2 li.navList__item: a(href="/#section01") section01 3 li.navList__item: a(href="/#section02") section02 4 li.navList__item: a(href="/#section03") section03
TOPページでは位置調整してくれるが下層ページから遷移すると動かない
index.js
1 $('a[href^="/#"],a[href^="#"]').on('click', function () { 2 href = $(this).attr('href').replace(/.*(#.*)/g, '$1'); 3 console.log(href); 4 if (href === '#' || href === '') { 5 target = $('html'); 6 } else { 7 target = $(href); 8 } 9 var adjust = -100; 10 position = target.offset().top + adjust; 11 console.log(position); 12 $('body, html').animate({ 13 scrollTop: position 14 }, 800, 'swing'); 15 return false; 16 });
回答1件
あなたの回答
tips
プレビュー