导航栏: 首页 评论列表

原生JS 网页滚动到底部自动加载

默认分类 2018/08/24 08:36

滑动到页面底部继续加载下一页

// 注:监听滚动事件
window.onscroll = function () {
  // 文档高度
  var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0
  if (document.body) {
    bodyScrollTop = document.body.scrollTop
  }
  if (document.documentElement) {
    documentScrollTop = document.documentElement.scrollTop
  }
  scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop
  // 可视窗口高度
  var windowHeight = 0
  if (document.compatMode == 'CSS1Compat') {
    windowHeight = document.documentElement.clientHeight
  } else {
    windowHeight = document.body.clientHeight
  }
  // 滚动条滚动高度
  var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0
  if (document.body) {
    bodyScrollHeight = document.body.scrollHeight
  }
  if (document.documentElement) {
    documentScrollHeight = document.documentElement.scrollHeight
  }
  scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight
  // 判断是否已经到底部
  if(scrollTop + windowHeight >= scrollHeight * 8 / 9){
    //当滚动条到底时,这里是触发内容
    //异步请求数据,局部刷新dom
    // ajax_function()
    // alert('bottom')
    loadNextTopic()
  }
}


>> 留言评论