的jquery如何偏移滚动位置
问题描述:
我有以下代码滚动到某一元素:的jquery如何偏移滚动位置
$('html, body').animate({
scrollTop: $("#maincontent").offset().top
}, 400);
然而,我有一个固定的导航栏的顶部和所以想能够抵消这一点。我怎样才能抵消一些像素?
答
尝试
$('html, body').animate({
scrollTop: $("#maincontent").offset().top - 100 // Means Less header height
},400);
+1
从偏移值中获得较少的标题高度,它将解决问题 – ShibinRagh 2013-05-10 11:55:30
答
$("#maincontent").scrollTop(300);
答
$("#maincontent").offset().top
只返回一个整数,你只需要增加或减少,以它来改变偏移
$("#maincontent").offset().top - 100
这可能帮助http://stackoverflow.com/questions/10297688/jquery-offset-values-changes-by-scroll-the-page – 2013-05-10 11:50:37