2017-09-18 3 views
0

スムーズなスクロールをウェブサイトのナビゲーションに追加しようとしていますが、スクロールが間違ったオフセットで開始され、 。スクロールはクロムのオフセットが間違っていますが、Firefoxではうまく動作します

$(window).on("scroll", function() { 
     var scroll_start = window.scrollY; 
     console.log(scroll_start); 
     if (scroll_start > offset) { 
      navToLight(); 
     } else { 
      navToDark(); 
     } 
}); 

これは私がNAVのアンカーをクリックしてイベントを処理する方法である:私はウェブサイトで、「についてのセクション」に移動しようとすると、

$('a.nav-link').on('click', function() { 
    var target = $($(this).attr('href')).position().top; 
    console.log("This is the target: "+target); 
    $("html, body").animate({ 
     scrollTop: target 
    }, 700); 
}); 

これは私がコンソールに得るものです。

Figure 1

Website demo

答えて

0

ブラウザはまた、あなたがやるべきことはイベントに伝播とのpreventDefaultを停止している、あなたのためのスクロール:

$('a.nav-link').on('click', function (e) { 
    e.preventDefault(); 
    e.stopPropagation(); 
    var target = $($(this).attr('href')).position().top; 
    console.log("This is the target: "+target); 
    $("html, body").animate({ 
    scrollTop: target 
    }, 700); 
}); 
関連する問題