1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > html+监听+页面滚动到底部 js 监听页面滚动到底部

html+监听+页面滚动到底部 js 监听页面滚动到底部

时间:2022-03-08 00:25:20

相关推荐

html+监听+页面滚动到底部 js 监听页面滚动到底部

直接上代码两种写法

1、jquery

Document

#contain{

border: 1px solid pink;

width: 100%;

height: 300px;

overflow: auto;

}

12345678

。。。。。。。。。。。。。。。。

198199200

scrollBottomTest =function(){

$("#contain").scroll(function(){

var $this =$(this),

viewH =$this.height(),//可见高度 $("#contain")[0].clientHeight

contentH =$this.get(0).scrollHeight,//内容高度

scrollTop =$this.scrollTop();//滚动高度

//if(contentH - viewH - scrollTop <= 100) { //到达底部100px时,加载新内容

if(scrollTop/(contentH -viewH)>=0.95){ //到达底部100px时,加载新内容

console.log('现在百分比是: ', scrollTop/(contentH -viewH))

}

});

}

scrollBottomTest()

2、原生写法

Document

#contain{

border: 1px solid pink;

width: 100%;

height: 300px;

overflow: auto;

}

12345678

。。。。。。。。。。。。。。。。

198199200

var _this = document.querySelector('#contain');

_this.addEventListener('scroll' , function(){

viewH = _this.clientHeight

contentH =_this.scrollHeight,//内容高度

scrollTop =_this.scrollTop;

//if(contentH - viewH - scrollTop <= 100) { //到达底部100px时,加载新内容

if(scrollTop/(contentH -viewH)>=0.95){ //到达底部100px时,加载新内容

console.log('现在百分比是: ', scrollTop/(contentH -viewH))

}

})

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。