js上拉跳转原理

时间:2023-03-09 21:37:08
js上拉跳转原理

今天遇到一个需要上拉跳转的地方,其原理跟上拉加载有点类似,代码如下

window.onscroll = function(){
if(getScrollTop() + getClientHeight() == getTotalHeight()) {
//执行跳转 ...对应你跳转的地址
window.location.href = "...";
}
}

  应用场景,移动端商品详情页面,通过上拉进入到图文详情。

  解释一下:

  getScrollTop():获取滚动条卷去高度;

  getClientHeight():获取可视区的高度;

  getTotalHeight():获取整个页面的整体高度,即滚动条的总长度。

  

//获取滚动条当前的位置
function getScrollTop() {
var scrollTop = 0;
if(document.documentElement && document.documentElement.scrollTop) {
scrollTop = document.documentElement.scrollTop;
} else if(document.body) {
scrollTop = document.body.scrollTop;
}
return scrollTop;
} //获取当前可视范围的高度
function getClientHeight() {
var clientHeight = 0;
if(document.body.clientHeight && document.documentElement.clientHeight) {
clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
} else {
clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
}
return clientHeight;
} //获取文档完整的高度
function getTotalHeight() {
return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}

  http://www.cnblogs.com/jiangbanji/p/6026755.html(这是我的后续文章)