javascript JQuery 窗口滚动顶部和底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20233046/
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
JQuery window scroll top and bottom
提问by Warz
I am able to load my ajax when scrolling all the way to the bottom, i am trying to figure out how i can modify the piece of code below so that it works only when the window is scrolled to the top ?
我可以在一直滚动到底部时加载我的 ajax,我想弄清楚如何修改下面的代码段,以便它仅在窗口滚动到顶部时才起作用?
$(window).scroll(function () {
if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
//this works here for scrolling bottom
}
else if ($(document).height() >= $(window).scrollTop() + $(window).height()){
//i tried checking for greater than the window scroll but that didn't owrk
}
});
回答by Md. Ashaduzzaman
When the scrollTop()returns the vertical position of the scroll bar 0it means the scroll bar is in top position.
当scrollTop()返回滚动条的垂直位置0 时,表示滚动条位于顶部位置。
$(window).scroll(function () {
if ($(window).scrollTop() == 0){
alert("Up");
}
});
Or you can update your code as follows,
或者您可以按如下方式更新您的代码,
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() < $(document).height()){
alert("Up");
//i tried checking for greater than the window scroll but that didn't work
}
});
回答by Ringo
Check this or perhaps you should check if height of document and window object to make sure they're not null.
检查这个或者你应该检查文档和窗口对象的高度以确保它们不为空。
$(window).scroll(function () {
if ($(document).height() <= Number($(window).scrollTop() + $(window).height())) {
//this works here for scrolling bottom
}
// only greater i think, not equa
else if ($(document).height() > Number($(window).scrollTop() + $(window).height())){
}
});