当前位置:网站首页>Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
2022-07-02 12:41:00 【By the Difeng River】
List of articles
Scroll Events
Event triggered when the page scrolls
- Why learn ?
Many web pages need to detect that users scroll the page to a certain area and do some processing , For example, fixed navigation bar , For example, back to the top - Event name :
scroll
- Monitor entire page scrolling :
towindow
ordocument
add toscroll
event
Listen to the internal scrolling of an element and add it directly to an element
window.addEventListener('scroll', function () {
console.log('scroll')
})
Loading event
Method 1
Load external resources ( Such as images 、 Outreach CSS and JavaScript etc. ) Event triggered when loading is complete
- Why learn ?
Sometimes you need to wait until all the page resources are processed and do something
Old code likes to put script Written in head in , At this time, go straight to dom Element not found - Event name :
load
- All resources on the listening page are loaded :
to window add toload
event
window.addEventListener('load', function () {
console.log('load')
})
- Be careful : You can not only listen to the whole page, but also when the resources are loaded , You can also bind to a resource
load
event
Method 2
- When the initial
HTML
After the document is fully loaded and parsed ,DOMContentLoaded
Event is triggered , Without waiting for the stylesheet 、 Images, etc. are fully loaded . - Event name :
DOMContentLoaded
- Monitor page DOM Loading finished :
todocument
add toDOMContentLoaded
event
window.addEventListener('DOMContentLoaded', function () {
console.log('DOMContentLoaded')
})
scroll family (scroll dimensions Rolling dimensions )
- Get wide height :
Fetch elementContent
Total width and height ( No scrollbars ) Return value without unitscrollWidth
andscrollHeight
- To obtain position :
Get the element content to the left 、 Roll up and out of sightscrollLeft
andscrollTop
These two attributes are Modifiable
div.addEventListener('scroll', function () {
console.log(this.scrollTop)
})
- scrollWidth and scrollHeight Is to get the width and height of elements ?
Content width and height , No scrollbars- The rolled out head or left side uses that attribute ? Can I read and modify ?
scrollTop / scrollLeft
Can read , You can also modify ( assignment )- Detect the head distance of page scrolling ( The rolled head ) Use that attribute ?
document.documentElement.scrollTop
The page scrolls back to the top button ( Case study )
Part of the code :
<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 () {
// Page scrolling detection
let num = document.documentElement.scrollTop
console.log(num)
if (num >= 500) {
backtop.style.display = 'block';
}
else {
backtop.style.display = 'none';
}
})
// Don't write in it , Low efficiency ( Scroll to monitor one time in turn )
backtop.children[1].addEventListener('click', function () {
document.documentElement.scrollTop = 0
})
</script>
</body>
offset family (offset dimensions Offset dimension )
- Get wide height :
Get the width and height of the element itself 、 Contains the width and height set by the element itself 、padding
、border
offsetWidth
andoffsetHeight
- To obtain position :
Gets the distance from the element to the left of the parent element 、 Up the distanceoffsetLeft
andoffsetTop
Note that Read-only property ( If you have to give value, usemargin-top,margin-left
Or in positioningtop and left
)
1.offsetWidth
andoffsetHeight
Is to get the width and height of elements ?
Content + padding + border
2.offsetTop
andoffsetLeft
Who gets the position ?
(1) Parent with positioning
(2) If there is none, take the upper left corner of the document Subject to
Elevator navigation case part code :
// 1. Get the yuan and take the element
let items = document.querySelectorAll('.item')
// Content box access
let neirongs = document.querySelectorAll('.neirong')
// 2. left aside modular Click who , Who highlights
for (let i = 0; i < items.length; i++) {
items[i].addEventListener('click', function () {
// Find the last one active Remove class
document.querySelector('.aside .active').classList.remove('active')
// Click who adds the class
this.classList.toggle('active')
// 3. Follow the content on the right Let the page scroll to the corresponding offsetTop Value position
// console.log(neirongs[i].offsetTop) Don't give it to the unit
document.documentElement.scrollTop = neirongs[i].offsetTop
})
}
client family (client dimensions Client size )
Browser background color change case part code
window.addEventListener('resize', function () {
//resize Monitor browser for window changes
let w = document.documentElement.clientWidth
console.log(w)
if (w >= 1920) {
// Browser visual window clientWidth If it changes, the background color changes
document.body.style.backgroundColor = 'blue'
}
else if (w > 540) {
document.body.style.backgroundColor = 'hotpink'
}
else {
document.body.style.backgroundColor = 'red'
}
})
summary
// scrollWidth scrollHeight Content Wide and high ( understand )
let div = document.querySelector('div')
console.log(div.scrollWidth) // 150 No unit
console.log(div.scrollHeight) // 336 No unit
console.log('----------------------------')
// offset The size of the box element = The width and height of the box itself + padding + border
console.log(div.offsetWidth) // 150 No unit
console.log(div.offsetHeight) // 150 No unit
// console.log(div.offsetTop) //
// console.log(div.offsetLeft)
// client Current viewable area No scrollbars Borders and so on
console.log('----------------------------')
console.log(div.clientWidth)
console.log(div.clientHeight)
console.log(div.clientTop) // The width of the border understand
console.log(div.clientLeft)
边栏推荐
- 线性DP AcWing 902. 最短编辑距离
- JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
- Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
- 8A 同步降压稳压器 TPS568230RJER_规格信息
- Sweetheart leader: Wang Xinling
- Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
- async/await 异步函数
- 基于STM32的OLED 屏幕驱动
- 线性DP AcWing 897. 最长公共子序列
- Sparkcontext: error initializing sparkcontext solution
猜你喜欢
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
arcgis js 4. Add pictures to x map
线性DP AcWing 896. 最长上升子序列 II
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
SparkContext: Error initializing SparkContext解决方法
堆 AcWing 838. 堆排序
AI中台技术调研
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
堆 AcWing 839. 模拟堆
随机推荐
IPhone 6 plus is listed in Apple's "retro products" list
接口测试面试题目,你都会了吗?
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
China traffic sign detection data set
上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
Redis introduction, scenario and data type
Deep Copy Event bus
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
Rust search server, rust quick service finding tutorial
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
Bom Dom
LeetCode—剑指 Offer 51. 数组中的逆序对
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
哈希表 AcWing 840. 模拟散列表
[ybtoj advanced training guide] similar string [string] [simulation]
防抖 节流
Drools executes the specified rule
Go学习笔记—基于Go的进程间通信
Fastdateformat why thread safe
Simple understanding of ThreadLocal