原生js 监听页面滚动到了底部

废话不多说,直接上代码

//监听页面的滚动
window.onscroll = function() {

	//scrollTop是滚动条滚动时,距离顶部的距离
	//为了保证兼容性,这里取两个值,哪个有值取哪一个
	let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
	//windowHeight是可视区的高度
	let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
	//scrollHeight是滚动条的总高度
	let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
    //滚动条到底部的条件
	if ((scrollTop + windowHeight) == scrollHeight) {
	    //到了这个就可以进行业务逻辑加载后台数据了
	}
}

猜你喜欢

转载自blog.csdn.net/rb0518/article/details/122165659