本文介绍了每次到达页面底部时加载内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试每次到达页面底部时加载内容. (类似于它在9GAG上的工作方式).这是我的代码:

I'm currently trying to load content every time the bottom of a page is reached. (Similiar to how it works on 9GAG).This is my code:

$(window).scroll(function() {
  if($(window).scrollTop() + $(window).height() > $(document).height() - 300) {
  $(window).unbind('scroll');

   $.get( "content.html", function( data ) {
       $("#div").append(data);
       $(window).bind('scroll');
    });
   }
});

显然,它只能运行一次,尽管我使用的是 $(window).bind('scroll'); .我有什么办法可以做到,所以每次都会加载?

Apparently it only works one time, altough I use $(window).bind('scroll');.Is there any way I can do it, so it loads every time?

编辑:问题是我使用> 而不是 == ,这导致了 $.get 会被解雇多次,如果我不取消绑定该事件的话............................................................................使用 == 可以正常工作.

EDIT: The problem was that I used > instead of ==, which caused the $.get to be fired multiple times, If I wouldn't have unbound the event. With == it works just fine.

推荐答案

这样做:

$(window).scroll(function () {

if ($(window).scrollTop() + $(window).height() == $(document).height())
   {

        $.get( "content.html", function( data ) {

        $("#div").append(data);

       });
   }
});

这是博客文章在我的应用程序中实现它后,我曾做过一次.

Here is the blog post i made once upon a time after implementing it in my application.

这篇关于每次到达页面底部时加载内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 15:51