1.新建一个lib文件夹
2.建一个base.js的文件(封装)
function onReachBottom(){ var scrollHeight=$(document).height(); var scrollTop=$(document).scrollTop(); var availHeight=$(window).clientHeight; return (Math.ceil(scrollTop+availHeight)==scrollHeight)?true:false;}
3.css样式
<script src="lib/base.js"></script> <style> *{margin: 0;padding: 0} body{ height: 1500px; background: red; } div{ height: 1000px; background: green; } </style>
<div> </div> <!-- 判断滚动条是否到达底部 --> <script> /*原生*/ /* var scrollHeight=document.documentElement.scrollHeight; console.log(scrollHeight) window.onscroll=function(){ var scrollTop=Math.ceil(document.documentElement.scrollTop);//四舍五入 var availHeight=document.documentElement.clientHeight; //屏幕的高度 if(scrollTop+availHeight==scrollHeight){ console.log("到达底部") } } */ window.onscroll=function(){ console.log(onReachBottom()) } </script>
4.原生
<div></div><script> var scrollHeight=document.documentElement.scrollHeight; console.log(scrollHeight) window.onscroll=function(){ var scrollTop=Math.ceil(document.documentElement.scrollTop);//四舍五入 var availHeight=document.documentElement.clientHeight; //屏幕的高度 if(scrollTop+availHeight==scrollHeight){ console.log("到达底部") } }</script>
5.Math
Math.ceil() 上取整Math.floor() 下取整Math.round() 四舍五入取整
var a=12.54; console.log(Math.ceil(a)) //13 console.log(Math.floor(a)) //12 console.log(Math.round(a)) //13