元素触底事件封装
在做移动端的时候经常需要用到触底加载,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)
})