繁体   English   中英

scrollTop不滚动到位置

[英]scrollTop not scrolling to position

我似乎无法滚动到设置变量,我使用的语法正确吗?

var offset = $(".box", this).offset().top;
$(window).animate({scrollTop:offset}, 500);

您无法滚动window 您无法为窗口倾斜设置动画(感谢Kato)。 尝试这个:

var offset = $(".box", this).offset().top;
$("html,body").animate({scrollTop:offset}, 500);

以下内容适用于IE和其他浏览器。

$('body,html').animate({scrollTop: offset}, 500);

offset()。top是相对于文档的距离。 因此,如果文档已全部滚动,则动画不正确。

$(“ body”)。animate({scrollTop:offset + $(“ body”)。scrollTop()},500);

我最终不得不使用position()而不是offset

var offset = item.position().top;
$("html, body").animate({scrollTop:offset}, 500);

通过阅读文档,建议使用注释,最好将$(“ html,body”)用于跨浏览器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM