【问题标题】:JQuery Mobile User scroll to bottomJQuery 移动用户滚动到底部
【发布时间】:2012-02-15 16:11:25
【问题描述】:

使用以下代码,我试图找到用户何时滚动到页面底部。在 JQuery 移动版中。

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

现在我只是希望它输出他们已经到达底部。

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

有没有办法在页面加载时停止它,并且仅在用户物理滚动页面时才这样做?

谢谢

【问题讨论】:

    标签: jquery jquery-mobile


    【解决方案1】:

    是因为你的内容比你的页面短吗?这意味着当它加载时,你已经在底部了。我试图在这里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");
            }
        }
    });
    

    【讨论】:

    • 是的,数据是动态加载的。所以初始加载它是空的,但是一旦数据被插入到 DOM 中,它显然会扩展。
    • 听起来您需要在加载内容后动态添加滚动。如果是这样,这是一个很好的资源stackoverflow.com/questions/4306387/…
    • 非常感谢!这让我发疯了!
    • @Rory:您能否发布您最终得到的解决方案?谢谢!
    【解决方案2】:

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

    http://jsfiddle.net/5x6T2/

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 2021-05-29
      • 2014-12-12
      • 2018-05-27
      • 2016-11-03
      相关资源
      最近更新 更多