当前位置:网站首页>35.滚动 scroll
35.滚动 scroll
2022-07-27 04:54:00 【Suyuoa】
目录
7 页面被卷去的上侧距离 window.pageYOffset
8 页面被卷去的左侧距离 window.pageXOffset
scroll与滚动条相关,主要有下面几个属性

与其他大小属性的区别如下

1 返回自身宽度 element.scrollWidth
scrollWidth返回的数值中包含padding,但不包含border与margin。scrollWidth返回的结果没有单位

![]()
- 123 = 103 + 10 * 2
2 返回自身高度 element.scrollHeight
scrollHeight返回的数值中包含padding,但不包含border与margin。scrollHeight返回的结果没有单位

![]()
- 186 = 166 + 10 * 2
3 sroll返回自身尺寸与client返回自身尺寸的区别

盒子中的内容超出了盒子本身尺寸的时候,scroll是按照内容走的,client不管内容只按照盒子原来的尺寸走

宽度如果超出和高度一样
4 被卷去的上侧距离 element.scrollTop
下面图的红色框子是div盒子的可视区域

把滚动条搞出来后,搞一个按钮进行监测

没滚动的时候是0



5 被卷去的左侧距离 element.scrollLeft
我们将div中的内容设置为不换行,然后搞个按钮监测



6 模仿淘宝侧边栏
打开之后的侧边栏是这样的

当滚到一定程度后会增加一个会顶部

代码是这样的
<!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>Document</title>
<style>
body {
height:3000px;
background-color: rgb(232,226,224);
}
.alongside {
position: fixed;
top: 50%;
right:0px;
transform: translate(0,-50%);
width:80px;
background-color: white;
border-radius: 20px 0px 0px 20px;
}
div {
height:60px;
display:flex;
flex-direction: column;
align-items: center;
font-size: 12px;
padding:5px;
}
.return_top {
display:none;
}
</style>
</head>
<body>
<section class="alongside">
<div>
<img src="images/官方客服.png" alt="">
<span>官方客服</span>
</div>
<div>
<img src="images/反馈.png" alt="">
<span>反馈</span>
</div>
<div>
<img src="images/举报.png" alt="">
<span>举报</span>
</div>
<div class="return_top">
<img src="images/回顶部.png" alt="">
<span>回顶部</span>
</div>
</section>
</body>
<script>
activate = document.querySelector('.activate')
return_top = document.querySelector('.return_top')
document.body.onscroll = function() {
console.log(document.documentElement.scrollTop)
if (document.documentElement.scrollTop > 500) {
return_top.style.display = 'flex';
}
else {
return_top.style.display = 'none';
}
}
</script>
</html>
打开后是这样的

滚动到一定的位置回出现回顶部

再滚到上面,回顶部消失

上面代码中使用了 document.documentElement,scrollTop 来获取页面滚动的上侧距离,除此之外还有另外两种方式

DTD是我们html最开始的这句话
![]()
可以搞一个照顾兼容性的函数,不过我觉得基本不会用到

7 页面被卷去的上侧距离 window.pageYOffset


8 页面被卷去的左侧距离 window.pageXOffset


边栏推荐
- 来自“飞人”乔丹的启示!奥尼尔开启的另一个“赛场”
- 勤于奋聊聊现在还有哪些副业可以做
- How to copy Photoshop layers to other documents
- TypeScript 详解
- R-score reproduction R-Precision evaluation index quantitative text generation image r-score quantitative experiment whole process reproduction (R-Precision) quantitative evaluation experiment step on
- Customize the viewport height, and use scrolling for extra parts
- Final Cut Pro Chinese tutorial (2) understanding of material window
- vim的基本操作
- Three paradigms, constraints, some keyword differences,
- Cache read / write policies: cacheside, read/writethrough and writeback policies
猜你喜欢

【搜索】Flood Fill 和 最短路模型

ps怎么导入lut预设?Photoshop导入lut调色预设教程

strlen和sizeof的区别

Photoshop各历史版本回顾以及系统要求

"Photoshop2021 tutorial" align and distribute to make dot patterns

Affine transformation module and conditional batch Standardization (CBN) of detailed text generated images

Photoshop裁剪工具隐藏技巧

CDH cluster integration external Flink (improved version - keep pace with the times)

Introduction to MySQL optimization

「Photoshop2021入门教程」“拉平”带有透视感的图像
随机推荐
安装Pygame
文件对话框
网络协议详解:IP
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
Photoshop裁剪工具隐藏技巧
Solution: how to use bash batch command in win10
Slashes / and backslashes involved in writing code\
When using Photoshop, the prompt "script error -50 general Photoshop error appears“
Test basis 5
Mysql表的约束
写代码涉及到的斜杠/和反斜杠\
柔性数组以及常见问题
Install pyGame
「Photoshop2021入门教程」对齐与分布制作波点图案
js小技巧
会议OA之我的审批
feign调用丢失请求头问题解决及原理分析
节流函数的demo——正则表达式匹配
传智教育|软件测试工程师未来的发展方向有哪些?
kali系统arp介绍(断网嗅探密码抓包)