滚动加载分页 上拉分页

本文介绍了一种实现网页滚动监听的方法,通过JavaScript函数获取页面滚动位置和窗口高度,当用户滚动到接近页面底部时触发加载更多内容的机制。涉及scrollTop、documentHeight和windowHeight等函数的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function scrollTop () {
  return Math.max(
    // chrome
    document.body.scrollTop,
    // firefox/IE
    document.documentElement.scrollTop)
}

function documentHeight () {
  // 现代浏览器(IE9+和其他浏览器)和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
  return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
}

function windowHeight () {
  return (document.compatMode == 'CSS1Compat')
    ? document.documentElement.clientHeight
    : document.body.clientHeight
}
// 上拉加载更多
var winH = $(window).height()
var checkTimer
$(document).scroll(function () {
  clearTimeout(checkTimer)
  checkTimer = setTimeout(function () {
    if (scrollTop() + windowHeight() >= (documentHeight() - 50)) {
      // 滚动分页ajax请求写在里面
    }
  }, 200)
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值