javascript 如何使用javascript找到当前窗口偏移量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7350146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find the current window offset using javascript?
提问by Rahul
When I open a page, the window offset would be 0 but when i scroll through the page, the offset of the window would increase correspondingly? How can I find the window's offset at any particular point of my web page?
当我打开一个页面时,窗口偏移量为0,但是当我滚动页面时,窗口的偏移量会相应增加吗?如何在我的网页的任何特定点找到窗口的偏移量?
回答by dtsn
You can get the scroll offset of the window
by using window.scrollX
& window.scrollY
. See the MDN articleon scrollY for more information.
您可以window
使用window.scrollX
&获取 的滚动偏移量window.scrollY
。有关更多信息,请参阅关于 scrollY的MDN 文章。
window.scrollY; // the current vertical scroll offset of the window
window.scrollY; // the current vertical scroll offset of the window
Just thought I would provide the non-jQuery version.
只是想我会提供非 jQuery 版本。
回答by Samich
I don't know if I get the question, but for getting scroll offset use:
我不知道我是否得到了这个问题,但是为了获得滚动偏移使用:
var scrollOffset = $(window).scrollTop();
回答by user5386084
The .offset()method is undefined for window objector window elementtherefore you should use the .scrollTop()method to solve your issue :)
该.offset()方法是未定义窗口对象或窗口元素,因此,你应该使用.scrollTop()方法来解决您的问题:)