当前位置:网站首页>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


边栏推荐
- CDH cluster integration external Flink (improved version - keep pace with the times)
- [search] two way search + A*
- [search] - multi source BFS + minimum step model
- Detailed explanation of mvcc and its principle
- Raspberry pie output PWM wave to drive the steering gear
- Review of various historical versions of Photoshop and system requirements
- Read write separation and master-slave synchronization
- 勤于奋聊聊现在还有哪些副业可以做
- How to do smooth data migration: Double write scheme
- [search] connectivity model of DFS + search order
猜你喜欢

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

When using Photoshop, the prompt "script error -50 general Photoshop error appears“
![[C language] detailed explanation of user-defined types (structure + enumeration + Union)](/img/d9/b10371159c63c126b5ff98bac0971a.png)
[C language] detailed explanation of user-defined types (structure + enumeration + Union)

OFDM 十六讲 2- OFDM and the DFT

Idea 如何新建一个groovy的项目(图文详细解释)

Network protocol details: IP

日落红暖色调调色滤镜luts预设Sunset LUTs 1

C语言 通讯录管理系统(链表,手机号码分段存储,txt文件存取,完整源码)
![[search] two way search + A*](/img/b1/115f75e8a7c7d3f797f5f4598a266c.png)
[search] two way search + A*

如何重置Photoshop首选项?ps重置首选项的方法
随机推荐
事件总结-常用总结
Stm32 cubemx hal----- PWM - change frequency
Reproduce ssa-gan using the nine day deep learning platform
C语言 通讯录管理系统(链表,手机号码分段存储,txt文件存取,完整源码)
OFDM 十六讲 2- OFDM and the DFT
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
ps怎么导入lut预设?Photoshop导入lut调色预设教程
背包问题dp
js小技巧
Counting Nodes in a Binary Search Tree
测试基础5
SVN使用详解
strlen和sizeof的区别
探寻通用奥特能平台安全、智能、性能的奥秘!
C language address book management system (linked list, segmented storage of mobile phone numbers, TXT file access, complete source code)
What if Photoshop prompts that the temporary storage disk is full? How to solve the problem that PS temporary storage disk is full?
项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知
Sub database and sub table
QT 菜单栏、工具栏和状态栏
安装Pygame