失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 如何用javascript判断滚动条到底部代码实例详解

如何用javascript判断滚动条到底部代码实例详解

时间:2019-07-22 20:55:16

相关推荐

如何用javascript判断滚动条到底部代码实例详解

web前端|js教程

javascript,js,底部

web前端-js教程

判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop、clientHeight、scrollHeight。

网上考试系统源码php,黑苹果 还是ubuntu,12306爬虫的意义,php 并发 flock,seo教学入门lzw

scrollTop为滚动条在Y轴上的滚动距离。

学校网站后台管理源码,ubuntu桌面下载apk,python 模拟点击爬虫,php .time(),ejs 有SEOlzw

clientHeight为内容可视区域的高度。

mqtt开发源码,vscode终端是干嘛的,ubuntu最佳配置,tomcat开启服务失败,eclipse爬虫插件,php判断时区,相城seo优化哪家好,jquery游戏网站模板,帝国cms办公模板lzw

scrollHeight为内容可视区域的高度加上溢出(滚动)的距离。

从这个三个属性的介绍就可以看出来,滚动条到底部的条件即为scrollTop + clientHeight == scrollHeight。

//滚动条在Y轴上的滚动距离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(patMode == "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");}});

如果要判断在某一个元素中的滚动条是否到底部,根据类似的思想,将document.body换成特定的元素即可,获取scrollTop和scrollHeight的方式是一样的,但是获取元素可见高度需要用到offsetHeight属性,直接依葫芦画瓢即可。

如果觉得《如何用javascript判断滚动条到底部代码实例详解》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。