当前位置:网站首页>浏览器渲染原理以及重排与重绘
浏览器渲染原理以及重排与重绘
2022-07-05 16:14:00 【爱划水de鲸鱼哥~】
浏览器渲染原理
浏览器渲染分为五个阶段。
第一阶段:HTML解析 --> DOM树
浏览器不能够直接理解 HTML文档,首先对其进行解析,并在解析过程中发出页面渲染所需的各种外部资源请求生成DOM树。
第二阶段: CSS解析 --> CSSOM树
同样浏览器也办法直接理解 CSS 代码,需要将识别并加载的CSS 代码处理成自己理解的 CSSOM 树。
第三阶段: 样式与结构合并 --> 渲染树
浏览器将DOM 树和 CSSOM 树结合在一起生成渲染树(:after :before 等伪元素会在这个环节被构建到 DOM 树中)。
渲染树特点:只包含渲染网页所需的节点。
构建渲染树的过程
- 第一步:从 DOM 树的根节点开始遍历,筛选出所有可见的节点;
- 第二步:对可见节点为其匹配 CSSOM树;
第四阶段: 页面布局 --> 布局渲染树
浏览器对渲染树进行遍历,将元素间嵌套关系以盒子模型的形式写入文档流盒模型,并在布局过程中会计算出元素确切的大小和定位。计算完毕后,将相应的信息写回渲染树上形成“布局渲染树”。
第五阶段: 页面绘制 --> 显示页面
在这一阶段浏览器终于拿到了绘制页面的所有信息,将渲染树上的每一个节点转换成肉眼可见的像素点,最终将页面呈现在我们面前。
重排
当我们修改DOM节点的宽、高或进行隐藏操作时,浏览器就需要重新计算节点的几何属性,然后再将计算的结果绘制出来的过程叫做重排(也叫回流)。
浏览器重排过程

触发重排的条件
- 改变 DOM 树的结构:节点的增减、移动等
- 获取一些需要通过"即时计算"得到值的属性:offsetWidth、offsetHeight、offsetTop、offsetLeft、 scrollTop、scrollLeft、scrollWidth、scrollHeight、clientTop、clientLeft、clientWidth、clientHeight 等属性以及调用 getComputedStyle 方法,或者 IE 的 currentStyle 方法。
重绘
当我们修改DOM的样式(未影响其几何属性)时,浏览器需要重新为元素绘制新的样式过程叫做重绘。
浏览器重绘过程

触发重绘的条件
修改DOM的样式,但未影响其几何属性会发生重绘。
优化
重排和重绘是比较消耗性能的,因此在开发过程中,我们应该避免重排和减少重绘。
本文到此结束
如果大家还有什么其他想法,欢迎在评论区交流!
边栏推荐
- Desci: is decentralized science the new trend of Web3.0?
- Twig数组合并的写法
- Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
- 二叉树相关OJ题
- Practice independent and controllable 3.0 and truly create the open source business of the Chinese people
- Games101 notes (III)
- PHP人才招聘系统开发 源代码 招聘网站源码二次开发
- 自己要有自己的坚持
- Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
猜你喜欢

Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)

极坐标扇图使用场景与功能详解

Jarvis OJ simple network management protocol

中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户

How to uninstall MySQL cleanly

Flet教程之 11 Row组件在水平数组中显示其子项的控件 基础入门(教程含源码)
![[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details

怎样在电脑上设置路由器的WiFi密码

解决CMakeList find_package找不到Qt5,找不到ECM

Games101 notes (III)
随机推荐
Cs231n notes (bottom) - applicable to 0 Foundation
Jarvis OJ Webshell分析
Summary of methods for finding intersection of ordered linked list sets
【刷題篇】鹅廠文化衫問題
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
DeSci:去中心化科学是Web3.0的新趋势?
Jarvis OJ 远程登录协议
JSON转MAP前后数据校验 -- 自定义UDF
关于new Map( )还有哪些是你不知道的
[深度学习][原创]让yolov6-0.1.0支持yolov5的txt读取数据集模式
时间戳strtotime前一天或后一天的日期
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
[es6] add if judgment or ternary operator judgment in the template string
一些认知的思考
Apple 已弃用 NavigationView,使用 NavigationStack 和 NavigationSplitView 实现 SwiftUI 导航
Cheer yourself up
Jarvis OJ Telnet Protocol
How to set the WiFi password of the router on the computer
Get ready for the pre-season card game MotoGP ignition champions!
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名