原生Js滑动页面底部加载更多-- (注:单页应用和非单页-写法不一样)

1. 原生JS(适用于PC端,iphone上有bug-- 推荐使用第二种) 

//用于计算-当前窗口的高度
function getClientHeight() {
    if(document.body.clientHeight&&document.documentElement.clientHeight)
    {
        return (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
    } else {
        return (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
    }
}

//监听滚动底部事件
function winScroll (){
    let scrollTop = document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;    //滚动条距离顶部的高度
    let scrollHeight = getClientHeight();   //当前页面的总高度
    let clientHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);    //当前可视的页面高度
    // console.log("top:"+scrollTop+",doc:"+scrollHeight+",client:"+clientHeight);
    if(Math.ceil(scrollTop +  scrollHeight) >= clientHeight){   //距离顶部+当前高度 >=文档总高度 即代表滑动到底部
        //调用分页功能等 滑动底部事件
        console.log('到底了');
    }
}

//给window添加监听
window.addEventListener('scroll', winScroll);//必要时移除监听 window.removeEventListener('scroll', winScroll, false)

 2. 第三方库,兼容移动端(推荐)

    使用详情见read.md

    https://github.com/StevenIseki/react-visit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值