如何获得文档的滚动位置?

时间:2022-06-03 07:32:39

How to get the scroll position value of a document?

如何获取文档的滚动位置值?

9 个解决方案

#1


46  

$(document).height() //returns window height

$(document).scrollTop() //returns scroll position from top of document

#2


176  

Here's how to get the scrollHeight of an element obtained using a jQuery selector:

以下是如何获取使用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.

如果选择器是元素的id(例如elemId),则可以保证数组的0索引项将是您希望选择的元素,并且滚动高度将是正确的。

#3


45  

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中获取值,但不是post 1.6。

#4


6  

document.getElementById("elementID").scrollHeight

$("elementID").scrollHeight

#5


6  

It uses HTML DOM Elements, but not jQuery selector. It can be used like:

它使用HTML DOM元素,但不使用jQuery选择器。可以这样使用:

var height = document.body.scrollHeight;

#6


3  

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..

每次你需要的时候就调用这个函数,这个警告是为了测试目的。

#7


3  

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”)返回未定义。

#8


0  

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})

#9


-1  

Try this:

试试这个:

var scrollHeight = $(scrollable)[0] == document ? document.body.scrollHeight : $(scrollable)[0].scrollHeight;

#1


46  

$(document).height() //returns window height

$(document).scrollTop() //returns scroll position from top of document

#2


176  

Here's how to get the scrollHeight of an element obtained using a jQuery selector:

以下是如何获取使用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.

如果选择器是元素的id(例如elemId),则可以保证数组的0索引项将是您希望选择的元素,并且滚动高度将是正确的。

#3


45  

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中获取值,但不是post 1.6。

#4


6  

document.getElementById("elementID").scrollHeight

$("elementID").scrollHeight

#5


6  

It uses HTML DOM Elements, but not jQuery selector. It can be used like:

它使用HTML DOM元素,但不使用jQuery选择器。可以这样使用:

var height = document.body.scrollHeight;

#6


3  

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..

每次你需要的时候就调用这个函数,这个警告是为了测试目的。

#7


3  

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”)返回未定义。

#8


0  

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})

#9


-1  

Try this:

试试这个:

var scrollHeight = $(scrollable)[0] == document ? document.body.scrollHeight : $(scrollable)[0].scrollHeight;