获取页面加载时窗口距文档顶部的距离(仅限 JavaScript)
我在计算页面加载时遇到了麻烦。它应该只需要一行,但我似乎无法理解。
I've been having trouble calculating this on page load. It should only take one line but I can't seem to get it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您能详细说明一下“窗口到文档的距离”是什么意思吗?如果您正在寻找屏幕高度/宽度:
或
垂直滚动位置使用:
我很确定它们是等效的,即
could you elaborate on what the "distance of window to document" means? if you are looking for screen height/width:
or
for vertical scroll position use:
I'm pretty sure they are equivalent i.e.
DSOC(文档滚动偏移坐标)可以通过 window.pageXOffset 和 window.pageYOffset 找到。就您而言,您需要 window.pageYOffset。
更多详细信息请参见此处:
http://www.javascriptkit.com/javatutors/static2.shtml
DSOC (document scroll offset coordinates) can be found with window.pageXOffset and window.pageYOffset. In your case, you want window.pageYOffset.
More details here:
http://www.javascriptkit.com/javatutors/static2.shtml
我意识到这是一个老问题,但我遇到了同样的问题并找到了解决方法。看来
window.scrollY
没有在页面加载时直接设置。但是,如果您执行以下操作,它将注册正确的值:我确定额外的 10 毫秒允许加载文档并设置
window.scrollY
值。I realize that this is an old question, but I had the same problem and sought out a way to fix it. It appears that
window.scrollY
is not set directly on page load. However, if you do the following, it will register the correct value:I've determined that the extra 10ms allows for the document to load and for it to set the
window.scrollY
value.