Jquery:页面从当前位置向下滚动 100px
如何将页面从当前位置向下移动 100 像素?
我的代码不起作用:
$('html,body').animate({
scrollTop: $(window).position().top + 100
})
像这样,但没有将页面滚动到特定元素,而是从窗口当前位置滚动 100px:
$('html, body').animate({
scrollTop: $("#scrollToHere").offset().top
}, 2000);
How do I animate the page down 100px from where it currently is?
my code doesn't work:
$('html,body').animate({
scrollTop: $(window).position().top + 100
})
like this, but without it scrolling the page to a specific element, instead, scrolling 100px from the windows current position:
$('html, body').animate({
scrollTop: $("#scrollToHere").offset().top
}, 2000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
scrollTop 属性告诉您元素从顶部开始放置的位置。您必须使用 window.scrollBy 方法和 window.scrollY 属性。不幸的是,您不能在 window.scrollY 属性上使用 animate,因为它是只读的。
您应该能够使用这样的东西:
速度与移动的像素数量有关。我稍后会尝试更新它。
The scrollTop property tells you where the element is placed from the top. You have to use the window.scrollBy method and the window.scrollY property. Unfortunately you can't use animate on the window.scrollY property since it's read-only.
You should be able to use something like this:
The speed is relavant to the amout of pixels moved. I'll try it to update it later.
我想到了scrollto插件:
http://flesler.blogspot.com/2007/10/jqueryscrollto.html
http://demos.flesler.com/jquery/scrollTo/
但也许有点矫枉过正为此:)
The scrollto plugin comes to mind:
http://flesler.blogspot.com/2007/10/jqueryscrollto.html
http://demos.flesler.com/jquery/scrollTo/
but maybe it's a bit overkill for that :)