当前位置:Gxlcms > JavaScript > 基于JQuery实现滚动到页面底端时自动加载更多信息

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

时间:2021-07-01 10:21:17 帮助过:3人阅读

关键代码:

代码如下:
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:

代码如下:
<div id="Loading">Loading...</div>

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

人气教程排行