好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解决HTML5中滚动到底部的事件问题

问题:在H5中,我们有这样的需求:例如有列表的时候,滚动到底部时,需要加载更多。

解决 方案 :可以采用window的滚动事件进行处理

分析:如果滚动是针对整个屏幕而言的(不针对于某个界面小块),那么这个 应该 是是成立的:屏幕的高度+最大滚动的 距离 = 内容的高度

代码实现:

&nbs p;

<ht ML > 
    <head> 
    < ;m eta charset="UTF-8">
        <t IT le>监听滚动到底部滚动底部</title> 
        <style> 
.div2{
width:100px;
h ei ght:100px;
border:1px solid  red 
}
*{
m arg in:0
}
.butto n1 :active{
   background:red
}
body{
height:375px;
width:667px;
border:1px solid red
}
.div1{
height:600px;
width:100%;
background:red
}
.div2{
height:600px;
width:100%;
background:green
}
.div3{
height:600px;
width:100%;
background:blue
}
.div4{
height:600px;
width:100%;
background:yellow
}
        </style> 
    </head> 
    <body > 
    <div class="div0">
    <div class="div1"></div>
    <div class="div2"></div>
    <div class="div3"></div>
    <div class="div4"></div>
    <div class="div5"></div>
    </div>
    </body> 
    <script>
    window.onload = function(){
  //获取容器父元素
     VAR  div0 = document.getElementsBy classname ('div0')[0];
    //height 计算属性的高度
    var height = parseInt((window.getComputedStyle(div0, null).height).replace('px', ''));
    console. LOG (height,"div0的计算高度")
    window.onscroll = function(){
/*
scrollTop 为滚动条顶端距离界面右上角的距离,这里采用了兼容性写法
*/
let scrollTop = document.documentElement  &&  document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
     //+-5是为了保证一定的弹性,并非要刚好相等才出发,
    if(height-5<=scrollTop+clientHeight& am p;&scrollTop+clientHeight<=height+5){
      console.log('监听成功','到达底部')
    }
    }
    }
    </script>
</html>

代码的相关说明:很多时候,列表加载,我们不能够把装载子元素的父容器高度设死,此时采用style设置为auto时, element.style.height 也会等于auto ,建议采用 clientHeight 或者 利用 计算样式 getComputedStyle 计算高度

总结

以上所述是小编给大家介绍的解决HTML5中滚动到底部的事件问题, 希望对大家有所帮助 ,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你 觉得 本文对你有帮助,欢迎 转载 ,烦请注明出处,谢谢!

总结

以上是 为你收集整理的 解决HTML5中滚动到底部的事件问题 全部内容,希望文章能够帮你解决 解决HTML5中滚动到底部的事件问题 所遇到的问题。

如果觉得 网站内容还不错, 推荐好友。

查看更多关于解决HTML5中滚动到底部的事件问题的详细内容...

  阅读:21次