美文网首页
JS 如何判断网页滚动到达了底部?

JS 如何判断网页滚动到达了底部?

作者: ER_PM | 来源:发表于2019-11-04 10:48 被阅读0次
    const handle = ()=>{
      let docEle = document.documentElement;
      let contentHeight = docEle.scrollHeight; //获取网页内容所占的高度
    
      // window.innerHeight 获取窗口的可见高度
      // docEle.scrollTop获取垂直滚动偏移量
       if(contentHeight  - docEle.scrollTop ==  window.innerHeight){
            //当滚动条到底时,docEle.scrollTop + window.innerHeight 跟contentHeight相等
            console.log('触底')
        }
    }
    //监听window的滚动事件
    window.addEventListener('scroll', handle);
    

    相关文章

      网友评论

          本文标题:JS 如何判断网页滚动到达了底部?

          本文链接:https://www.haomeiwen.com/subject/tvvrbctx.html