JavaScriptH5移动端滚动加载下一页(附带防抖函数,原生写法,JQuery写法)

原生写法

<script>
    /**
  * 防抖函数
  * @param method 事件触发的操作
  * @param delay 触发之后多长时间后执行
  * @returns {Function}
  */
    function debounce(method, delay) {
        let timer = null;
        return function () {
            let self = this, args = arguments;
            clearTimeout(timer);
            timer = setTimeout(function () {
                method.apply(self, args);
            }, delay);
        }
    }

    // 加载函数
    function delFn() {
        console.log('执行了');
    }

    //取进度条到底部距离
    var getScrollTop = function () {
        var scrollTop = 0;
        if (document.documentElement && document.documentElement.scrollTop) {
            scrollTop = document.documentElement.scrollTop;
        } else if (document.body) {
            scrollTop = document.body.scrollTop;
        }
        return scrollTop;
    };
    
    //取页面可视区域高度
    var getClientHeight = function () {
        var clientHeight = 0;
        if (document.body.clientHeight && document.documentElement.clientHeight) {
            clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
        } else {
            clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
        }
        return clientHeight;
    };
    
    //取文档整体高度
    var getScrollHeight = function () {
        return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
    };
    
    //页面滚动事件
    window.onscroll = debounce(function () {
        if (getScrollTop() + getClientHeight() > (getScrollHeight() - 200)) {
            //页面已滚动到最底部
            // fun();//页面已滚动到最底部处理
            delFn();
        }
    }, 250)
    </script>

JQuery版本

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
    $(window).scrollTop();  //获取的是滚动条的垂直偏移 
    $(document).height();  //获取的是文档的高度 
    $(window).height();   //获取的是窗口的可视区域的高度
    /**
 * 防抖函数
 * @param method 事件触发的操作
 * @param delay 触发之后多长时间后执行
 * @returns {Function}
 */
    function debounce(method, delay) {
        let timer = null;
        return function () {
            let self = this, args = arguments;
            clearTimeout(timer);
            timer = setTimeout(function () {
                method.apply(self, args);
            }, delay);
        }
    }
    
    // 加载函数
    function delFn() {
        console.log('执行了');
    }
    
    //页面滚动事件
    $(window).scroll(debounce(function () {
        //判断是否滑动到页面底部
        if ($(window).scrollTop() + $(window).height() > $(document).height() - 200) {
            // 滑动到底部时可请求下一页的数据并加载,加载可使用append方法
            delFn();
        }
    }, 250));
    </script>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

词不达意难知

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值