当前位置:网站首页>Web APIs DOM page special effects scrolling events and loading events
Web APIs DOM page special effects scrolling events and loading events
2022-07-21 00:48:00 【Dark horse programmer official】
Notes updated in the early stage : Web API Basic cognition / obtain DOM Elements / Set up / modify DOM Element content and element attributes / Timer - Intermittent function / The basis of the event / Higher order function / Environment object / Comprehensive case -Tab Bar Toggle / DOM node /DOM Time object /DOM Redraw and reflow / DOM- Event object /DOM- Flow of events / DOM- Event delegation + Comprehensive case
One 、 Scroll events and load events
1.1 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 :
- to window or document add to scroll event
- Listen to the internal scrolling of an element and add it directly to an element
Page scrolling event code :
<!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; } div { overflow: auto; width: 200px; height: 200px; background-color: pink; } </style>
</head>
<body>
<div>
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
</div>
<script> let div = document.querySelector('div') window.addEventListener('scroll', function () { console.log(111) }) // div.addEventListener('scroll', function () {
// console.log(111) // }) </script>
</body>
</html>
1.2 Loading event
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 to load event
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
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 :
- to document add to DOMContentLoaded event
Load event code :
<!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; } div { overflow: auto; width: 200px; height: 200px; background-color: pink; } </style>
<script> window.addEventListener('load', function () { let div = document.querySelector('div') console.log(div) }) </script>
</head>
<body>
<div>
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
I can put a lot of words in it
</div>
<script> // let div = document.querySelector('div') </script>
</body>
</html>
Summary :
1. How to add page scrolling events ?
- scroll
- Listen to the whole page scroll to window or document Add
2. What are the two loading events ? How to add ?
load event
- Listen to the entire page resources to window Add
DOMContentLoaded
- to document Add , When the initial HTML The document is fully loaded and parsed after ,DOMContentLoaded Event is triggered , Without waiting for the style surface 、 Images, etc. are fully loaded
Dark horse front-end column has a lot of dry goods , Focus on relearning , It's convenient ~
2022 Front end learning roadmap : Course 、 Source code 、 note , Technology stack In addition, the circuit diagram is updated in real time ! Friends who need after-school materials , You can tell me directly .
边栏推荐
- 央视新闻《宁波开餐饮手撕定额发票》新闻频道_人民网
- 央视新闻《武汉开住宿手撕定额发票》新闻频道_人民网
- Linux CentOS 7 installation mysql8.0.19
- CCTV news news "Suzhou restaurant manual tearing quota invoice" news channel_ People's network
- 央视新闻《苏州开餐饮手撕定额发票》新闻频道_人民网
- LeetCode-数组中数字出现的次数(单身狗问题)
- EasyCVR平台针对360浏览器自动填充密码问题的解决办法
- 【JVM 系列】JVM 四大引用和对象的判断
- 二进制安装MySQL5.7
- CCTV news "Chengdu rent quota invoice by hand" news channel_ People's network
猜你喜欢
10. Démarrage rapide du moteur
Protocol Buffer 学习
Automatic analysis of Luogu p1955 program
C asynchronous programming read this article is enough
Win11如何开启任务栏多样化?Win11开启新任务栏的方法
怎么使用mysql的导出语句
How to get asp Net core current startup address?
信息系统项目管理师核心考点(四十六)采购工作说明书(SOW)
EasyCVR配置RTMP推流后重启导致RTMP离线该如何解决?
Codeworks 5 questions per day (average 1500) - day 20
随机推荐
CCTV news "Qingdao opens catering quota invoice by hand" news channel_ People's network
Chinese Mainland IP segment (including Hong Kong and Macao) [2022-07-15]
Learun,已开源,一个.net web快速开
央视新闻《重庆开餐饮手撕定额发票》新闻频道_人民网
央视新闻《郑州开餐饮手撕定额发票》新闻频道_人民网
DeiT:注意力也能蒸馏
Codeworks 5 questions per day (average 1500) - day 20
央视新闻《宁波开餐饮手撕定额发票》新闻频道_人民网
Protocol Buffer 学习
央视新闻《天津开餐饮手撕定额发票》新闻频道_人民网
央视新闻《苏州开餐饮手撕定额发票》新闻频道_人民网
信息系统项目管理师核心考点(四十六)采购工作说明书(SOW)
Main research directions of Intelligent Question Answering
软件测试知识库+1,5款顶级自动化测试工具推荐和使用分析
[swpu2019] web1-1 | SQL injection
Sweetalert notes - add input box pictures, etc. in the pop-up window
10、gin快速入門
Web性能测试需求分析,具体应该怎么做?
How can easycvr solve RTMP offline caused by restarting after configuring RTMP streaming?
央视新闻《北京开住宿手撕定额发票》新闻频道_人民网