为什么这个分页自动加载代码不能正常工作?

发布于 2024-12-20 17:02:31 字数 326 浏览 1 评论 0原文

当用户滚动到页面底部时,我试图进行一些分页,加载更多内容。有没有办法让说几乎到页面底部?比如页面底部的 1/3?

$(window).scroll(function(){
    if ($(window).scrollTop() == ($(document).height() - $(window).height())/3)
    {
        alert('test');
    }
    // ...

这没有任何作用

,但是如果我删除代码中的 3,当我到达底部时(死胡同,无法再滚动),就会弹出警报。但我希望在我快要触底时显示警报。

I'm trying to do some pagination when user scrolls to the bottom of the page, more content loads. Is there a way to get to say almost to the bottom of the page? Like maybe 1/3 from the bottom of the page?

$(window).scroll(function(){
    if ($(window).scrollTop() == ($(document).height() - $(window).height())/3)
    {
        alert('test');
    }
    // ...

This doesn't do anything

But if I delete that 3 in the code, alert pops when I get at the bottom (dead end, cant scroll anymore). But I want the alert to show when I'm almost to the bottom.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我只土不豪 2024-12-27 17:02:31

问题是:

  • 您检查它是否完全相等。这种情况很少见。
  • 您检查一下它是否位于页面下方的 1/3rd,而不是 2/3rds

试试这个 - http://jsfiddle.net/y7Am2/

$(window).scroll(function(){
    var mostOfTheWayDown = ($(document).height() - $(window).height()) * 2 / 3;
    if ($(window).scrollTop() >= mostOfTheWayDown)
    {
        alert('test');
    }
});

但我建议您使用底部的特定像素值,而不是顶部的小数值。将其基于页脚的高度(简单的手动调整值或计算值)。

您的内容可能不是恒定的高度。如果没有,并且您使用小数值,您将滚动到页面的任意部分,并且加载将排队。这将使您的网站看起来不可预测,或者如果用户习惯于在较长的页面上较早加载,则可能会降低响应速度。

因此,请执行类似的操作 - http://jsfiddle.net/PwZ4D/

$(window).scroll(function(){
    var mostOfTheWayDown = ($(document).height() - $(window).height())
        - 150 - footerHeight;
    // ...

另请注意,目前实施的每次您在页面底部滚动时,您的事件都会持续触发。这将使大量 AJAX 调用排队。您可能应该在事件中取消绑定 scroll ,然后在 ajax 调用成功完成时重新绑定它。

并小心处理内容很少或没有内容的情况。如果页面是空的或者只有足够的数据来填满一个屏幕,那么如果您做错了事情,它可能会继续不必要地重新查询服务器。

编辑:

根据请求,您可以使用如下代码取消绑定 scroll 事件 - http://jsfiddle.net/5vXJ5/

$(window).unbind('scroll');

编辑:

再次根据请求,您以与开始绑定它相同的方式重新绑定它。但是,由于您最终得到的是自引用代码,因此您需要命名您的函数。最终的代码可能看起来像这样:

var scrollHandler;
scrollHandler = function(){
  var mostOfTheWayDown = ($(document).height() - $(window).height()) * 2 / 3;

  if ($(window).scrollTop() >= mostOfTheWayDown)
  {
    // So we don't queue up multiple requests until the first one completes
    $(window).unbind('scroll');

    $.ajax({
      url: "test.html",
      context: document.body,
      success: function(){
        $(window).scroll(scrollHandler); // re-bind after it completes
      }
    });
  }
};

$(window).scroll(scrollHandler); // bind by named function

但这只是一个示例。您必须根据您的具体情况对其进行调整。

The problem is:

  • You check if it is exactly equal. This is going to be rare
  • You check if it is 1/3rd down the page, not 2/3rds

Try this - http://jsfiddle.net/y7Am2/:

$(window).scroll(function(){
    var mostOfTheWayDown = ($(document).height() - $(window).height()) * 2 / 3;
    if ($(window).scrollTop() >= mostOfTheWayDown)
    {
        alert('test');
    }
});

But I recommend you use a specific pixel value from the bottom rather than a fractional value from the top. Base it on the height of your footer (a simple hand-tweaked value, or calculated).

Your content might not be constant height. If not, and you use a fractional value, you will scroll to what seems like an arbitrary part of the page and the loading will queue. This will make your website seem unpredictable, or possibly less responsive if users are used to it loading earlier on longer pages.

So do something like this instead - http://jsfiddle.net/PwZ4D/:

$(window).scroll(function(){
    var mostOfTheWayDown = ($(document).height() - $(window).height())
        - 150 - footerHeight;
    // ...

Also note that as currently implemented your event will keep firing every time you scroll around at the bottom of the page. This will queue up a whole ton of AJAX calls. You probably should unbind scroll inside your event, then rebind it when your ajax call finishes successfully.

And be careful to handle cases where you have little to no content. If a page is empty or there is only enough data to fill one screen, it could keep re-querying the server needlessly if you do things wrong.

Edit:

Per request, you unbind the scroll event with code like this - http://jsfiddle.net/5vXJ5/:

$(window).unbind('scroll');

Edit:

Again per request, you rebind it the same way you bound it to begin with. However, since you end up with self-referential code you'll need to name your function. The final code might look something like this:

var scrollHandler;
scrollHandler = function(){
  var mostOfTheWayDown = ($(document).height() - $(window).height()) * 2 / 3;

  if ($(window).scrollTop() >= mostOfTheWayDown)
  {
    // So we don't queue up multiple requests until the first one completes
    $(window).unbind('scroll');

    $.ajax({
      url: "test.html",
      context: document.body,
      success: function(){
        $(window).scroll(scrollHandler); // re-bind after it completes
      }
    });
  }
};

$(window).scroll(scrollHandler); // bind by named function

This is just an example though. You'll have to tweak it to your specific scenario.

浅浅淡淡 2024-12-27 17:02:31

我认为下面的计算应该更适合您正在寻找的情况 window.scrollTop max is document.height - window.height

$(window).scrollTop() = $(document).height() - $(window).height() - $(window).height())/3

I think the calculation below should be more correct for what you are seeking for the case window.scrollTop max is document.height - window.height:

$(window).scrollTop() = $(document).height() - $(window).height() - $(window).height())/3
我喜欢麦丽素 2024-12-27 17:02:31

您可以使用插件,例如

Endless Scroll或
无限滚动

或者直接编写自己的

$(window).scroll(function () { 
   if ($(window).scrollTop() >= ($(document).height() - $(window).height()) - 100) {
      console.log('load more!')
   }
});

You can either use a plugin such as

Endless Scroll or
Infinite Scroll

Or simply write your own

$(window).scroll(function () { 
   if ($(window).scrollTop() >= ($(document).height() - $(window).height()) - 100) {
      console.log('load more!')
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文