jquery如何偏移滚动位置

jquery如何偏移滚动位置,jquery,scroll,Jquery,Scroll,我有以下滚动到某个元素的代码: $('html, body').animate({ scrollTop: $("#maincontent").offset().top }, 400); 然而,我在顶部有一个固定的导航条,所以我希望能够抵消这一点。如何将其偏移若干像素?试试看 $('html, body').animate({ scrollTop: $("#maincontent").offset().top - 100 // Means Less heade

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

$('html, body').animate({
         scrollTop: $("#maincontent").offset().top
     }, 400);
然而,我在顶部有一个固定的导航条,所以我希望能够抵消这一点。如何将其偏移若干像素?

试试看

$('html, body').animate({
    scrollTop: $("#maincontent").offset().top - 100 // Means Less header height
},400);
试一试

$(“#maincontent”).offset().top
只返回一个整数,只需对其进行加减即可更改偏移量

$("#maincontent").offset().top - 100
$(“#maincontent”).offset().top
只返回一个整数,只需对其进行加减即可更改偏移量

$("#maincontent").offset().top - 100

这可能有帮助这可能有助于从偏移值减少收割台高度,这将解决从偏移值减少收割台高度的问题,这将解决问题