下拉加载更多
发布时间:2020-12-16 03:03:17 所属栏目:百科 来源:网络整理
导读:需求说明:当用户浏览到页面底部时候,自动加载下一页的内容 实现原理:JS获取当前滚动条高度、滚动条长度以及页面总长度,当滚动条高度加上当前滚动条长度等于页面总长度的时候,页面到达底部,此时可以触发ajax加载下一页内容。代码如下 script //获取滚动
<script> //获取滚动条当前的位置 function getScrollTop() { var scrollTop = 0; if (document.documentElement && document.documentElement.scrollTop) { scrollTop = document.documentElement.scrollTop; } else if (document.body) { scrollTop = document.body.scrollTop; } return scrollTop; } //获取当前可视范围的高度 function getClientHeight() { 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; } //获取文档完整的高度 function getScrollHeight() { return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight); } window.onscroll = function () { if (getScrollTop() + getClientHeight() == getScrollHeight()) { $.ajax({}) }</script> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |