在之前从未了解过这种情况,曾以为都是一样的使用
clientHeight+scrollTop == scrollHeight都能兼容这些浏览器,但是最近做一个底部加载发现并没有用
通过对一些资料的查询,得出一个兼容到手机QQ浏览器的到底部的代码
function getScrollTop(){
var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
if(document.body){
bodyScrollTop = document.body.scrollTop;
}
if(document.documentElement){
documentScrollTop = document.documentElement.scrollTop;
}
scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
return scrollTop;
}
//文档的总高度
function getScrollHeight(){
var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
if(document.body){
bodyScrollHeight = document.body.scrollHeight;
}
if(document.documentElement){
documentScrollHeight = document.documentElement.scrollHeight;
}
scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
return scrollHeight;
}
window.addEventListener('scroll', function() {
if(getScrollTop() + window.innerHeight == getScrollHeight()) {
alert('已经到底')
}
})