主视口的正确 HTML 滚动元素 (+jQuery)
当我想通过 javascript 代码滚动网站时,要设置 .scrollTop
属性的正确元素是什么?
是 window
、document
、document.body
还是 html
元素?
jQuery 是否有一些技巧来检测滚动主视口的意图并统一调用?在我看来,有时 $(window).scroolTop(value)
有效,但有时却无效。
When I want to scroll a website by javascript code what is the corrent element to set the .scrollTop
property on?
Is it window
, document
, document.body
or html
element?
Does jQuery have some trick to detect the intention to scroll the main viewport and unify the calls? It seems to me that sometimes $(window).scroolTop(value)
works, but sometimes it doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,
元素,又名
document.documentElement
。但是,如果您使用 Quirks 模式,则(
document.body
) 代表视口。 (HTML/CSS 没有明确声明/要求这一点,但这是所有现代浏览器的工作方式。)您永远不想处于 Quirks 模式,但如果您正在编写一个包含在其他人的页面上的脚本,您就会发现必须处理它:
jQuery 使用
window.scrollTo()
而不是直接设置滚动属性。无论哪种方式在实践中都几乎相同。您可能会说使用scrollTo
更简洁,因为它避免了依赖于特定元素表示的视口,但 jQuery 仍然必须使用scrollTop
/scrollLeft
无论如何都可以读取“other”属性的当前值,所以没有什么大的胜利。您可以使用pageXOffset
/pageYOffset
来读取当前滚动位置,而不依赖于特定元素,但并非所有地方都支持它,因此您仍然必须回退到scrollTop
方法。遗憾的是,这些属性/方法过去都没有标准化。 CSSOM 视图最终解决了这个问题。
Normally, the
<html>
element, akadocument.documentElement
. However if you are using Quirks Mode,<body>
(document.body
) represents the viewport instead. (HTML/CSS doesn't explicitly state/require this, but it is how all modern browsers work.)You don't ever want to be in Quirks Mode, but if you're writing a script for inclusion on other people's pages you will have to deal with it:
jQuery uses
window.scrollTo()
instead of setting the scroll properties directly. Either way is pretty much the same in practice. You could argue that usingscrollTo
is a bit cleaner in that it avoids relying on the viewport being represented by a particular element, but then jQuery still has to usescrollTop
/scrollLeft
to read the current value of the ‘other’ property anyway, so no big win. You can usepageXOffset
/pageYOffset
to read the current scroll position without relying on a particular element, but it's not supported everywhere so you still have to fallback to thescrollTop
method.Sadly none of these properties/methods have been standardised in the past. CSSOM Views finally address this.
bobince 的答案有一个附加条件,即 Webkit 浏览器错误地将 documentElement.scrollTop 设置为零,无论您处于怪异模式还是标准模式;因此,你不能完全依赖他的俏皮话。
https://bugs.webkit.org/show_bug.cgi?id=9248
http://code.google.com/p/chromium/issues/detail ?id=2891
拥有足够权限的人应该将此评论作为 bobince 答案的评论,这样就不会错过,然后我就可以删除这篇文章。
There's a rider to bobince's answer, which is that Webkit browsers incorrectly set documentElement.scrollTop to zero regardless of whether you are in quirks or standards mode; therefore, you cannot completely rely on his one-liner.
https://bugs.webkit.org/show_bug.cgi?id=9248
http://code.google.com/p/chromium/issues/detail?id=2891
Someone with sufficient privileges should make this a comment to bobince's answer so that it is not missed, and I can then delete this post.
取决于您滚动的内容,文档/窗口或分区
Depends on what you are scrolling, document/Window or a division
我以前也遇到过这个问题,并且它在许多浏览器中的表现完全不同。我最终使用了这段相当可怕的代码:
但是,我只需要滚动到顶部,这更容易。如果您想滚动到特定点,我不确定您会如何执行此操作。也许使用锚点?
I had this problem before as well, and it behaved quite differently in a number of browsers. I ended up using this rather horrible piece of code:
However, I only needed to scroll to the top, which is easier. I'm not sure how you would do this if you want to scroll to a specific point. Maybe use anchors?