jQuery如何偏移滚动位置

bab*_*eii 0 jquery scroll

我有以下代码滚动到某个元素:

$('html, body').animate({
         scrollTop: $("#maincontent").offset().top
     }, 400);
Run Code Online (Sandbox Code Playgroud)

但是,我在顶部有一个固定的导航栏,因此希望能够抵消这一点。如何将其偏移多个像素?

Shi*_*agh 5

尝试

$('html, body').animate({
    scrollTop: $("#maincontent").offset().top - 100 // Means Less header height
},400);
Run Code Online (Sandbox Code Playgroud)