javascript 测量窗口偏移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6422561/
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
Measuring the window offset
提问by Mild Fuzz
Is there a way to measure the offset of the window in jQuery, in order than I might compare the positions of a 'fixed' element and a relatively positioned one?
有没有办法在 jQuery 中测量窗口的偏移量,以便比较“固定”元素和相对定位元素的位置?
I need to be able to tell how far the window is scrolled so I can use the figure to calculate the difference between the height of the fixed element (which is relative to the viewport top) and the relative object (which is relative to the top of the document)
我需要能够知道窗口滚动了多远,以便我可以使用该图来计算固定元素(相对于视口顶部)和相对对象(相对于顶部)的高度之间的差异文件)
回答by David Wick
$(window).scrollTop()
and $(window).scrollLeft()
can be used to find scroll positions.
$(window).scrollTop()
并且$(window).scrollLeft()
可用于查找滚动位置。
回答by kevinAlbs
Using plain Javascript without jQuery should be window.pageXOffset
and window.pageYOffset
, which return the number of pixels offset by scrolling.
使用没有 jQuery 的普通 Javascript 应该是window.pageXOffset
and window.pageYOffset
,它返回通过滚动偏移的像素数。
Source: https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY
来源:https: //developer.mozilla.org/en-US/docs/Web/API/Window/scrollY
回答by Gaurav Agrawal
document.body.offsetWidth;
document.body.offsetWidth;
and
和
document.body.offsetHeight;
document.body.offsetHeight;
By using these javascript functions you can get actual width and height of the browser window
通过使用这些 javascript 函数,您可以获得浏览器窗口的实际宽度和高度