jQuery 平滑滚动偏移量

4
我有以下的jQuery代码。
$(document).ready(function(){
    $('a[href^="#"]').on('click',function (e) {
        e.preventDefault();

        var target = this.hash,
        $target = $(target);

        $('html, body').stop().animate({
            'scrollTop': $target.offset().top
        }, 900, 'swing', function () {
            window.location.hash = target;
        });
    });
});

这是一个滚动插件,可以平稳地向页面内部链接滚动。我想将学校偏移-140px,这样它就不会直接滚动到DIV。如何实现?
1个回答

13

尝试...

$('html, body').stop().animate({
    'scrollTop': $target.offset().top-140
}, 900, 'swing', function () {
    window.location.hash = target;
});

1
应该知道,在某些情况下,因为在这个例子中同时选择了html和body,回调函数会触发两次。 - MLK.DEV

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接