当前位置:网站首页>js监听页面或元素scroll事件,滚动到底部或顶部
js监听页面或元素scroll事件,滚动到底部或顶部
2022-06-24 19:44:00 【彭世瑜】

基本原理:
1、滚动到底部
元素的滚动距离 + 元素的可视距离 == 元素的滚动条总距离
2、滚动到顶部
元素的滚动距离 == 0
监听页面滚动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Scroll Demo</title>
</head>
<body>
<style> .box {
height: 5000px; text-align: center; } </style>
<div class="box" id="box">打开控制台查看</div>
<!-- 引入节流方法 -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/index.min.js"></script>
<script> // 滚动方向枚举值 const DIRECTION_ENUM = {
DOWN: "down", UP: "up", }; // 距离顶部或底部的阈值 const threshold = 20; // 记录前一个滚动位置 let beforeScrollTop = 0; function handleScroll() {
// 距顶部 var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 可视区高度 var clientHeight = document.documentElement.clientHeight || document.body.clientHeight; // 滚动条总高度 var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; // 打印数值 console.table([ {
label: "距顶部", value: scrollTop, }, {
label: "可视区高度", value: clientHeight, }, {
label: "滚动条总高度", value: scrollHeight, }, {
label: "距顶部 + 可视区高度", value: scrollTop + clientHeight, }, ]); // 确定滚动方向 let direction = DIRECTION_ENUM.DOWN; if (beforeScrollTop > scrollTop) {
direction = DIRECTION_ENUM.UP; } // 通过滚动方向判断是触底还是触顶 if (direction == DIRECTION_ENUM.DOWN) {
// 滚动触底 if (scrollTop + clientHeight + threshold >= scrollHeight) {
console.log("滚动触底"); } } else {
// 滚动到顶部 if (scrollTop <= threshold) {
console.log("滚动到顶部"); } } beforeScrollTop = scrollTop; } // 滚动节流 const throttleHandleScroll = throttleDebounce.throttle( 1000, handleScroll ); // 监听滚动 window.addEventListener('scroll', throttleHandleScroll); </script>
</body>
</html>
同理,也可以监听元素的滚动
<style> .box-wrap {
height: 500px; overflow-y: auto; } .box {
height: 5000px; text-align: center; } </style>
<div class="box-wrap" id="box">
<div class="box">打开控制台查看</div>
</div>
<script> // 监听滚动 let box = document.querySelector("#box"); box.addEventListener("scroll", function (e) {
let scrollTop = e.target.scrollTop; let clientHeight = e.target.clientHeight; let scrollHeight = e.target.scrollHeight; // 打印数值 console.table([ {
label: "距顶部", value: scrollTop, }, {
label: "可视区高度", value: clientHeight, }, {
label: "滚动条总高度", value: scrollHeight, }, {
label: "距顶部 + 可视区高度", value: scrollTop + clientHeight, }, ]); }); </script>
判断触底需要注意的点:
- 滚动时需要区分向上滚动还是向下滚动
- 滚动时可以设置一个阈值,并非完全触底或触顶才触发
- 滚动事件需要做节流操作,以免短时间内被多次触发
在线Demo
边栏推荐
- golang map clear
- EMI的主要原因-工模电流
- 【js】-【数组应用】-学习笔记
- Laravel pagoda security configuration
- R language uses GLM function to build Poisson log linear regression model, processes three-dimensional contingency table data to build saturation model, uses summary function to obtain model summary s
- 7-2 后序+中序序列构造二叉树
- Building Survey [3]
- 7-8 梯云纵
- golang convert json string to map
- What good smart home brands in China support homekit?
猜你喜欢

宁德时代定增450亿:高瓴认购30亿 曾毓群仍控制23%股权

选择类排序法

【js】-【树】-学习笔记
15 lines of code using mathematical formulas in wangeditor V5

国内有哪些好的智能家居品牌支持homekit?
![[JS] - [array, stack, queue, linked list basics] - Notes](/img/c6/a1bd3b8ef6476d7d549abcb442949a.png)
[JS] - [array, stack, queue, linked list basics] - Notes

File contains vulnerability issues
![[introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy](/img/d0/7d78b00e4f6ad1e8efb73a5d472b09.png)
[introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy

Ganglia 的安装与部署

#22Map介绍与API
随机推荐
Jetpack Compose 最新进展
golang convert map to json string
7-8 循环日程安排问题
Use of laravel verifier
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the AIC function to compare the AIC values of the two models (simpl
Still using simpledateformat for time formatting? Be careful of project collapse
冒泡排序
7-9 寻宝路线
华为机器学习服务语音识别功能,让应用绘“声”绘色
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉
Building Survey [2]
Basic data type
选择类排序法
Helix distance of point
Installation and deployment of ganglia
Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine
Force deduction solution summary 515- find the maximum value in each tree row
【js】-【栈、队-应用】-学习笔记
379. 捉迷藏
Financial management [4]