jQuery使用window.location和负边距平滑滚动

时间:2019-06-06 06:56:36

标签: javascript jquery smooth-scrolling

我正在尝试为固定的导航创建平滑滚动,以将新的哈希值保留在url中,并允许导航高度。问题是由于window.location.hash = hashscrollTop: $(hash).offset().top - $('.menucontent').height()冲突,有一个小的跳转广告会滚动到该位置。有人知道解决方法吗?

$('a').on( 'click', function(e)  {
    if ( this.hash !== '' ) {
        e.preventDefault();

        var hash = this.hash;

        $('html, body').animate({
            scrollTop: $(hash).offset().top - $('.menucontent').height()
        }, 800, function() {
            window.location.hash = hash;
        });
    }
});

0 个答案:

没有答案
相关问题