当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 基于JQuery实现滚动到页面底端时自动加载更多信息

基于JQuery实现滚动到页面底端时自动加载更多信息

2019年03月21日  | 移动技术网IT编程  | 我要评论

关键代码:

. 代码如下:


var stop=true;
$(window).scroll(function(){
    totalheight = parsefloat($(window).height()) + parsefloat($(window).scrolltop());
    if($(document).height() <= totalheight){
        if(stop==true){
            stop=false;
            $.post("ajax.php", {start:1, n:50},function(txt){
                $("#loading").before(txt);
                stop=true;
            },"text");
        }
    }
});

 

html:

 

. 代码如下:


<p id="loading">loading...</p>

实现方法是比较页面总高度和下滚高度以判断是否到达底端,若到达底端则通过ajax读取更多的内容,用before插入到loading之前。
stop参数是考虑到ajax读取耗时,防止在一次ajax读取过程中多次触发事件,造成多次加载内容。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网