JQuery Mobile User滚动到底部

时间:2012-02-15 16:11:26

标签: jquery jquery-mobile

使用以下代码,我试图找到用户滚动到页面底部的时间。在JQuery mobile。

$(window).scroll(function(){
     if($(window).scrollTop() == $(document).height() - $(window).height()){
          alert("The Bottom");
     }
});

现在我只是想让它输出它们已经到了底部。

我的问题是,当网站加载时,它会输出此消息。当我滚动到底部时,它将输出警报。

有没有办法阻止它在页面加载时执行,只在用户实际滚动页面时才执行此操作?

由于

2 个答案:

答案 0 :(得分:7)

是因为您的内容比您的网页短吗?这意味着当它加载时,你已经在底部了。我试图在这里复制你的问题http://jsfiddle.net/qESXR/2/,它的行为就像你想要的那样。但是,如果我缩短内容并在我的机器上本地运行,我会得到相同的结果 如果是这样,您可以使用这些

检查页面的高度与html的高度
$(window).height();   // returns height of browser viewport

$(document).height(); // returns height of HTML document
像这样:

$(window).scroll(function(){
    if($(document).height() > $(window).height())
    {
        if($(window).scrollTop() == $(document).height() - $(window).height()){
          alert("The Bottom");
        }
    }
});

答案 1 :(得分:1)

问题在于jQuery Mobile的page widget将每个“页面”视为滚动的窗口。要检测用户何时滚动到最后,请将滚动功能绑定到$(document)

http://jsfiddle.net/5x6T2/

$(document).scroll(function () {
    if ($(window).scrollTop() + $(window).height() == $(document).height()) {
        alert("Bottom reached!");
    }
});
相关问题