当前位置:网站首页>JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
2022-07-02 09:43:00 【荻风溪畔】
文章目录
滚动事件
当页面进行滚动时触发的事件
- 为什么要学?
很多网页需要检测用户把页面滚动到某个区域后做一些处理, 比如固定导航栏,比如返回顶部 - 事件名:
scroll - 监听整个页面滚动:
给window或document添加scroll事件
监听某个元素的内部滚动直接给某个元素加即可
window.addEventListener('scroll', function () {
console.log('scroll')
})
加载事件
方法1
加载外部资源(如图片、外联CSS和JavaScript等)加载完毕时触发的事件
- 为什么要学?
有些时候需要等页面资源全部处理完了做一些事情
老代码喜欢把 script 写在 head 中,这时候直接找 dom 元素找不到 - 事件名:
load - 监听页面所有资源加载完毕:
给 window 添加load事件
window.addEventListener('load', function () {
console.log('load')
})
- 注意:不光可以监听整个页面资源加载完毕,也可以针对某个资源绑定
load事件
方法2
- 当初始的
HTML文档被完全加载和解析完成之后,DOMContentLoaded事件被触发,而无需等待样式表、图像等完全加载。 - 事件名:
DOMContentLoaded - 监听页面DOM加载完毕:
给document添加DOMContentLoaded事件
window.addEventListener('DOMContentLoaded', function () {
console.log('DOMContentLoaded')
})
scroll家族(scroll dimensions 滚动尺寸)
- 获取宽高:
获取元素的内容总宽高(不包含滚动条)返回值不带单位scrollWidth和scrollHeight - 获取位置:
获取元素内容往左、往上滚出去看不到的距离scrollLeft和scrollTop
这两个属性是 可以修改的
div.addEventListener('scroll', function () {
console.log(this.scrollTop)
})

- scrollWidth和scrollHeight是得到元素什么的宽高?
内容宽高,不包含滚动条- 被卷去的头部或者左侧用那个属性?是否可以读取和修改?
scrollTop / scrollLeft
可以读取,也可以修改(赋值)- 检测页面滚动的头部距离(被卷去的头部)用那个属性?
document.documentElement.scrollTop
页面滚动显示返回顶部按钮(案例)

部分代码:
<body>
<div class="content"></div>
<div class="backtop">
<img src="./images/close2.png" alt="">
<a href="javascript:;"></a>
</div>
<script>
let backtop = document.querySelector('.backtop')
window.addEventListener('scroll', function () {
//页面滚动检测
let num = document.documentElement.scrollTop
console.log(num)
if (num >= 500) {
backtop.style.display = 'block';
}
else {
backtop.style.display = 'none';
}
})
//不要写在里面,效率低(滚动依次监听一次)
backtop.children[1].addEventListener('click', function () {
document.documentElement.scrollTop = 0
})
</script>
</body>
offset家族(offset dimensions偏移尺寸)

- 获取宽高:
获取元素的自身宽高、包含元素自身设置的宽高、padding、borderoffsetWidth和offsetHeight - 获取位置:
获取元素距离自己定位父级元素的左、上距离offsetLeft和offsetTop注意是只读属性(非要给值就用margin-top,margin-left或者定位中的top和left)
1.offsetWidth和offsetHeight是得到元素什么的宽高?
内容 + padding + border
2.offsetTop和offsetLeft得到位置以谁为准?
(1)带有定位的父级
(2)如果都没有则以文档左上角 为准
电梯导航案例部分代码:
// 1. 获元取素
let items = document.querySelectorAll('.item')
// 内容的盒子获取
let neirongs = document.querySelectorAll('.neirong')
// 2. 左侧aside 模块 点击谁,谁高亮
for (let i = 0; i < items.length; i++) {
items[i].addEventListener('click', function () {
// 找到上一个active 移除类
document.querySelector('.aside .active').classList.remove('active')
// 点击谁谁添加类
this.classList.toggle('active')
// 3. 右侧内容跟随走动 让页面滚动到对应的offsetTop值位置
// console.log(neirongs[i].offsetTop) 不用给单位
document.documentElement.scrollTop = neirongs[i].offsetTop
})
}
client家族(client dimensions客户端尺寸)

浏览器背景颜色改变案例部分代码
window.addEventListener('resize', function () {
//resize 监听浏览器可是窗口变化
let w = document.documentElement.clientWidth
console.log(w)
if (w >= 1920) {
//浏览器可视窗口clientWidth发生变化则背景颜色改变
document.body.style.backgroundColor = 'blue'
}
else if (w > 540) {
document.body.style.backgroundColor = 'hotpink'
}
else {
document.body.style.backgroundColor = 'red'
}
})
总结

// scrollWidth scrollHeight 内容 宽高 (了解)
let div = document.querySelector('div')
console.log(div.scrollWidth) // 150 不带单位
console.log(div.scrollHeight) // 336 不带单位
console.log('----------------------------')
// offset 盒子元素的大小 = 盒子本身的宽度和高度 + padding + border
console.log(div.offsetWidth) // 150 不带单位
console.log(div.offsetHeight) // 150 不带单位
// console.log(div.offsetTop) //
// console.log(div.offsetLeft)
// client 当前可视区域 不包含滚动条 边框等等
console.log('----------------------------')
console.log(div.clientWidth)
console.log(div.clientHeight)
console.log(div.clientTop) // 边框的宽度 了解
console.log(div.clientLeft)
边栏推荐
- kubenetes中port、targetPort、nodePort、containerPort的区别与联系
- Sort---
- Intel 内部指令 --- AVX和AVX2学习笔记
- MySQL and PostgreSQL methods to grab slow SQL
- 高性能纠删码编码
- 趣味 面试题
- 1380. Lucky numbers in the matrix [two-dimensional array, matrix]
- 堆 AcWing 839. 模拟堆
- Lekao: 22 year first-class fire engineer "technical practice" knowledge points
- 哈希表 AcWing 840. 模拟散列表
猜你喜欢

Adding database driver to sqoop of cdh6

线性DP AcWing 898. 数字三角形

Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)

Tas (file d'attente prioritaire)
![[ybtoj advanced training guide] similar string [string] [simulation]](/img/eb/acfefc7f85018fe9365d13502e2b0a.jpg)
[ybtoj advanced training guide] similar string [string] [simulation]

Go学习笔记—多线程

drools中then部分的写法

CDA data analysis -- Introduction and use of aarrr growth model

堆(优先级队列)

Drools dynamically add, modify, and delete rules
随机推荐
CDA数据分析——Excel数据处理的常见知识点归纳
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
Shutter encapsulated button
[C language] convert decimal numbers to binary numbers
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
BOM DOM
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
arcgis js 4. Add pictures to x map
Record the range of data that MySQL update will lock
怎样写一篇赏心悦目的英文数学论文
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
spfa AcWing 851. spfa求最短路
Intel internal instructions - AVX and avx2 learning notes
[C language] Yang Hui triangle, customize the number of lines of the triangle
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Drools executes the specified rule
MySQL indexes and transactions
Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
Brush questions --- binary tree --2
堆(優先級隊列)