jQuery 如何获取文档的滚动位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3407133/
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 get the scroll position of a document?
提问by Somebody
How to get the scroll position value of a document?
如何获取文档的滚动位置值?
采纳答案by Bob Gregor
$(document).height() //returns window height
$(document).scrollTop() //returns scroll position from top of document
回答by Demwis
Here's how to get the scrollHeight
of an element obtained using a jQuery selector:
以下是scrollHeight
获取使用 jQuery 选择器获得的元素的方法:
$(selector)[0].scrollHeight
If selector
is the id of the element (e.g. elemId
), it is guaranteed that the 0-indexed item of the array will be the element you wish to select, and scrollHeight
will be correct.
如果selector
是元素的 id(例如elemId
),则保证数组的 0 索引项将是您希望选择的元素,并且scrollHeight
是正确的。
回答by Ankit
If you are using Jquery 1.6 or above, use prop to access the value.
如果您使用的是 Jquery 1.6 或更高版本,请使用 prop 访问该值。
$(document).prop('scrollHeight')
Previous versions used to get the value from attr but not post 1.6.
以前的版本用于从 attr 获取值,但不是 1.6 之后的版本。
回答by aqua
It uses HTML DOM Elements, but not jQuery selector. It can be used like:
它使用 HTML DOM 元素,但不使用 jQuery 选择器。它可以像这样使用:
var height = document.body.scrollHeight;
回答by Sachin R
document.getElementById("elementID").scrollHeight
$("elementID").scrollHeight
回答by acjay
To get the actual scrollable height of the areas scrolled by the window scrollbar, I used $('body').prop('scrollHeight')
. This seems to be the simplest working solution, but I haven't checked extensively for compatibility. Emanuele Del Grande notes on another solution that this probably won't work for IE below 8.
为了获得窗口滚动条滚动区域的实际可滚动高度,我使用了$('body').prop('scrollHeight')
. 这似乎是最简单的工作解决方案,但我没有广泛检查兼容性。Emanuele Del Grande 指出另一个解决方案,这可能不适用于 IE 低于 8。
Most of the other solutions work fine for scrollable elements, but this works for the whole window. Notably, I had the same issue as Michael for Ankit's solution, namely, that $(document).prop('scrollHeight')
is returning undefined
.
大多数其他解决方案适用于可滚动元素,但这适用于整个窗口。值得注意的是,对于 Ankit 的解决方案,我遇到了与 Michael 相同的问题,即$(document).prop('scrollHeight')
返回undefined
.
回答by Zuul
Something like this should solve your problem:
这样的事情应该可以解决您的问题:
$.getDocHeight = function(){
var D = document;
return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
};
alert( $.getDocHeight() );
Ps: Call that function every time you need it, the alert is for testing purposes..
Ps:每次需要时调用该函数,警报用于测试目的..
回答by Dmitry Komin
Try this:
尝试这个:
var scrollHeight = $(scrollable)[0] == document ? document.body.scrollHeight : $(scrollable)[0].scrollHeight;
回答by Steve
You can try this for example, this code put the scrollbar at the bottom for all DIV tags
例如,您可以试试这个,这段代码将滚动条放在所有 DIV 标签的底部
Remember: jQuery can accept a function instead the value as argument. "this" is the object treated by jQuery, the function returns the scrollHeight property of the current DIV "this" and do it for all DIV in the document.
请记住:jQuery 可以接受一个函数而不是值作为参数。“this”是jQuery处理的对象,该函数返回当前DIV“this”的scrollHeight属性,并对文档中的所有DIV进行处理。
$("div").scrollTop(function(){return this.scrollHeight})