当前位置:网站首页>Web APIs DOM- 网页特效篇-滚动事件和加载事件
Web APIs DOM- 网页特效篇-滚动事件和加载事件
2022-07-20 00:45:00 【黑马程序员官方】
前期更新笔记内容: Web API 基本认知 / 获取DOM元素 / 设置/修改DOM元素内容和元素属性 / 定时器-间歇函数 / 事件基础 / 高阶函数 / 环境对象 / 综合案例-Tab栏切换 / DOM节点 /DOM 时间对象/DOM 重绘和回流/ DOM- 事件对象/DOM- 事件流 / DOM- 事件委托 +综合案例
一、滚动事件和加载事件
1.1 滚动事件
当页面进行滚动时触发的事件
为什么要学?
- 很多网页需要检测用户把页面滚动到某个区域后做一些处理, 比如固定导航栏,比如返回顶部
事件名:scroll
监听整个页面滚动:
- 给 window 或 document 添加 scroll 事件
- 监听某个元素的内部滚动直接给某个元素加即可
页面滚动事件代码:
<!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>
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
</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 加载事件
加载外部资源(如图片、外联CSS和JavaScript等)加载完毕时触发的事件
为什么要学?
- 有些时候需要等页面资源全部处理完了做一些事情
- 老代码喜欢把 script 写在 head 中,这时候直接找 dom 元素找不到
事件名:load
监听页面所有资源加载完毕:
- 给 window 添加 load 事件
注意:不光可以监听整个页面资源加载完毕,也可以针对某个资源绑定load事件
当初始的 HTML 文档被完全加载和解析完成之后,DOMContentLoaded 事件被触发,而无需等待样式表 、图像等完全加载
事件名:DOMContentLoaded
监听页面DOM加载完毕:
- 给 document 添加 DOMContentLoaded 事件
加载事件代码:
<!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>
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
我里面可以放很多的文字
</div>
<script> // let div = document.querySelector('div') </script>
</body>
</html>
小结:
1. 页面滚动事件如何添加?
- scroll
- 监听整个页面滚动给 window 或 document 加
2. 加载事件有哪两个?如何添加?
load 事件
- 监听整个页面资源给 window 加
DOMContentLoaded
- 给 document 加,当初始的 HTML 文档被完全加载和解析完成之后,DOMContentLoaded 事件被触发,而无需等待样式表、图像等完全加载
边栏推荐
- 开发那些事儿:EasyCVR集群该如何配置参数?
- Luogu p5250 timber warehouse
- 11. Gin Middleware
- CCTV news "Qingdao opens catering quota invoice by hand" news channel_ People's network
- JDBC功能类详解
- Create the future and enjoy extraordinary. Opengauss Developer Day 2022 was successfully held
- Leetcode palindrome linked list
- Luogu p4305 does not repeat numbers
- Why has API strategy become a magic weapon for enterprises' digital transformation?
- CCTV news news news channel of Shenzhen catering manual tearing quota invoice_ People's network
猜你喜欢
Edge developer salon | one hour proficient in edge extension development
[swpu2019] web1-1 | SQL injection
Create the future and enjoy extraordinary. Opengauss Developer Day 2022 was successfully held
C#异步编程看这篇就够了
10. Démarrage rapide du moteur
吴恩达-02 改善深层神经网络:超参数调试、正则化以及优化
Wechat applet development learning 4 (view and logic)
全局事件总线概述
这款国产良心软件正式开源!
Recommend an open source mall
随机推荐
央视新闻《南京开餐饮手撕定额发票》新闻频道_人民网
Overwintering samples of game companies: has going to sea and boutique become a new growth point?
[romance understood by technical talents] tidb community has prepared a "Tanabata Festival" gift for your partner, and everyone has a share!
Speech signal processing: sampling theorem [as long as the sampling frequency is greater than or equal to twice the maximum frequency of the effective signal, the sampling value can contain all the in
浅析云边端协同架构在安防视频领域的应用意义及场景
Wechat applet development learning 4 (view and logic)
Create the future and enjoy extraordinary. Opengauss Developer Day 2022 was successfully held
EasyCVR配置RTMP推流后重启导致RTMP离线该如何解决?
CCTV news "Shanghai rent quota invoice by hand" news channel_ People's network
央视新闻《武汉开餐饮手撕定额发票》新闻频道_人民网
吴恩达-02 改善深层神经网络:超参数调试、正则化以及优化
Detailed explanation of JDBC function classes
How can easycvr solve RTMP offline caused by restarting after configuring RTMP streaming?
智能问答(Question Answering)的主要研究方向
央视新闻《南京开住宿手撕定额发票》新闻频道_人民网
Web性能测试需求分析,具体应该怎么做?
NET问答: C# 中是否有最高效的方式对大文件做 checksum ?
Dest0g3 520 orientation Web
CCTV news news news channel of Shenzhen catering manual tearing quota invoice_ People's network
央视新闻《杭州开住宿手撕定额发票》新闻频道_人民网