jQuery 如何使用jquery检测垂直滚动位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18023903/
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 to detect vertical scroll position using jquery?
提问by omega
Using jquery, how can you set an event that triggers when there is a vertical scroll visible, and when you change from top half of page to bottom half of page, and vice versa.
使用jquery,如何设置在垂直滚动可见时触发的事件,以及从页面上半部分更改到页面下半部分时触发的事件,反之亦然。
So for example, if the vertical scroll bar is there, and then I am looking somewhere on the page which is in the top half of the page, and then move down so I am in the bottom half of the page, the function happens. Then if I move back up so I am in the top half again, the function happens.
因此,例如,如果垂直滚动条在那里,然后我正在查看页面上半部分的页面上的某个位置,然后向下移动到页面的下半部分,该功能就会发生。然后,如果我向上移动,再次回到上半部分,该功能就会发生。
Thanks.
谢谢。
回答by raam86
var midHeight = jQuery(window).height() / 2 //Splits screen in half
$(window).scroll(function () {
if ($(window).scrollTop() > midHeight) {
//Do something on bottom
} else {
//Do something on top
}
})
回答by revlayle
well jQuery does have these methods you can use on an element:
jQuery 确实有这些方法可以在元素上使用:
.scrollTop()- get top scroll position on an element in the viewport
.scrollTop()- 获取视口中元素的顶部滚动位置
.scrollLeft()- get left scroll position on an element in the viewport
.scrollLeft()- 在视口中的元素上获得左滚动位置
Also there is the scroll event - that triggers when a viewport/element scrolls within another one (or the window):
还有滚动事件 - 当视口/元素在另一个(或窗口)内滚动时触发:
回答by softvar
$(window).scroll(function () {
if ($(window).scrollTop() > $('body').height() / 2) {
//code goes here...
}
});
Detect Scroll
检测滚动
var s = $('body').scrollTop();
jQuery scrollTop()
jQuery滚动顶部()
Description: Get the current vertical position of the scroll bar for the first element in the set of matched elements or set the vertical position of the scroll bar for every matched element.
描述:获取匹配元素集合中第一个元素的滚动条当前垂直位置或为每个匹配元素设置滚动条的垂直位置。
You may be interested to know:
您可能有兴趣了解:
$().scrollTop()//how much has been scrolled
$().innerHeight()// inner height of the element
DOMElement.scrollHeight//height of the content of the element