JS 判断滚动条到底部的JS代码
来源:互联网 发布:石田电子称软件 编辑:程序博客网 时间:2024/05/21 17:33
判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop、clientHeight、scrollHeight。
scrollTop为滚动条在Y轴上的滚动距离。
clientHeight为内容可视区域的高度。
scrollHeight为内容可视区域的高度加上溢出(滚动)的距离。
从这个三个属性的介绍就可以看出来,滚动条到底部的条件即为scrollTop + clientHeight == scrollHeight。
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;
}
//浏览器视口的高度
function getWindowHeight(){
var windowHeight = 0;
if(document.compatMode == "CSS1Compat"){
windowHeight = document.documentElement.clientHeight;
}else{
windowHeight = document.body.clientHeight;
}
return windowHeight;
}
window.onscroll = function(){
if(getScrollTop() + getWindowHeight() >= getScrollHeight()){
alert("you are in the bottom!");
}
};
====jquery用法====
$(window).scroll(function(){
var scrollTop = $(this).scrollTop();
var scrollHeight = $(document).height();
var windowHeight = $(this).height();
if(scrollTop + windowHeight == scrollHeight){
alert("you are in the bottom");
}
});
//根据某个节点来操作
var _h=0,_st=0,_sh=0;
_st=$(this)[0].scrollTop;_sh=$(this)[0].scrollHeight;if(h+_st>=_sh){ console.log("buttom");}
由于zeptojs对scrolltop错在一些问,可以通过javascript实现
- JS 判断滚动条到底部的JS代码
- 判断滚动条到底部的JS代码
- 判断滚动条到底部的JS代码
- 判断滚动条到底部的JS代码
- js 判断滚动条滚动到底部
- js判断滚动条是否到底部
- JS如何判断滚动条是否滚到底部
- JS如何判断滚动条是否滚到底部
- JS如何判断滚动条是否滚到底部
- 001 - JS 如何判断滚动条是否滚到底部
- js判断滚动条是否滚到底部
- JS判断滚动条到底部懒加载
- JS如何判断滚动条是否滚到底部
- js 判断滚动条是否到达底部
- js监听滚动条滑动到底部
- JS及jQuery获取各种宽度、高度的简单介绍以及判断滚动条滚动到底部事件
- 判断滚动条到底部的方法
- js滚动到顶部底部代码
- 《实体解析与信息质量》-2.1.2信息质量和信息品质
- 高质量C/C++编程指南(四)
- Lua中使用openGL shader时,Scale9Sprite无法变灰色
- Android学习 - 悬浮控件
- 几种地图投影方式
- JS 判断滚动条到底部的JS代码
- rubymine 入门简单例子教程
- 正式入驻CSDN
- java.lang.NoSuchMethodException: com.sun.proxy.$Proxy
- ios8 远程推送的注册代码
- c1 驾考成都龙岗科目三考试经验
- 免安装版apache注册window服务
- 论文图片编辑
- Why is machine learning used heavily for Google's ad ranking and less for their search ranking?