js 监听页面滚动到底部,监听可视区域滚动到底部
全局页面body上的底部监听:window.onscroll = function(){var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;var windowHeight = document.documentElement.clientHeight || document.body.client
·
全局页面body上的底部监听:
window.onscroll = function(){
var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
var scrollHeight = document.documentElement.scrollHeight||document.body.scrollHeight;
if(scrollTop+windowHeight>=scrollHeight){ //考虑到滚动的位置一般可能会大于一点可滚动的高度,所以这里不能用等于
console.log("距顶部"+scrollTop+"可视区高度"+windowHeight+"滚动条总高度"+scrollHeight);
}
}
局部可视区域的滚动到底部(vue版本代码):
<div style="flex:1;overflow-y:auto;" @scroll="scrollEvent">内容</div>
methods: {
// 滚定监听
scrollEvent(e) {
// console.log('滚动',e.currentTarget.scrollTop,e.currentTarget.clientHeight,e.currentTarget.scrollHeight);
if (Math.ceil(e.currentTarget.scrollTop + e.currentTarget.clientHeight) >=e.currentTarget.scrollHeight) { //容差:20px
// console.log('滚动到底部');
this.$emit('scrollBottom');
}
}
},
更多推荐
已为社区贡献42条内容
所有评论(0)