JQ封装触底事件,滚动条触底加载更多

元素触底事件封装

在做移动端的时候经常需要用到触底加载,jq也没有封装好事件,这里自己封装了一个触底事件,方便绑定元素调用


// 触底事件
$.prototype.touchGround = function (touchGround, scroll) {
  this.scroll(function () {
    let scroll_height = $(this)[0].scrollHeight//可滚动高度
    let el_height = $(this).innerHeight()//元素高度
    let scroll_top = $(this).scrollTop()//滚动高度
    scroll && scroll(scroll_top)
    if (scroll_height - (el_height + scroll_top) <= 0) {
      touchGround && touchGround()
    }
  })
}

使用方法

需要注意的是这边绑定的元素(例子:class=“test”),css需要设一个固定高度,超出滚动

$('.test').touchGround(function(){
	console.log('触底啦~')
},function(distance){
	cosnole.log('滚动距离:' + distance)
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值