当前位置:网站首页>Relationship between redrawing and reflow
Relationship between redrawing and reflow
2022-07-29 05:40:00 【Try your best】
Need to know : Browsers use a streaming layout model (Flow Based Layout).
The browser will HTML It can be interpreted as DOM, hold CSS It can be interpreted as CSSOM,DOM and CSSOM The merger produces Render Tree.
With RenderTree, We know the style of all nodes , Then calculate their size and location on the page , Finally, draw the node on the page .
Because browsers use streaming layout , Yes Render Tree We usually only need to traverse it once , but table And its internal elements , They may need to calculate many times , It usually takes 3 Times the time of the same element , That's why we should avoid using table One of the reasons for the layout .
In a word : Reflow must cause redrawing , Redrawing does not necessarily cause reflow
When Render Tree The size of some or all of the elements in 、 structure 、 Or when some properties change , The process of the browser re rendering part or all of the document is called reflow .
Operation leading to backflow :
Page first render
Browser window size changes
A change in the size or position of an element
Element content changes ( The number of words or the size of pictures and so on )
Element font size changes
Add or remove visible DOM Elements
Activate CSS pseudo-classes ( for example ::hover)
Query some properties or call some methods
Some common properties and methods that lead to backflow :
clientWidth、clientHeight、clientTop、clientLeft
offsetWidth、offsetHeight、offsetTop、offsetLeft
scrollWidth、scrollHeight、scrollTop、scrollLeft
scrollIntoView()、scrollIntoViewIfNeeded()
getComputedStyle()
getBoundingClientRect()
scrollTo()
Repaint (Repaint)
When an element style change in a page does not affect its position in the document flow ( for example :color、background-color、visibility etc. ), The browser assigns the new style to the element and redraws it , This process is called redrawing .
Performance impact
The price of reflow is higher .
Sometimes even if only a single element is refluxed , Its parent element and any elements that follow it also have a backflow .
Modern browsers optimize frequent reflow or redraw operations :
The browser maintains a queue , Put all operations that cause backflow and redraw in the queue , If the number of tasks in the queue or the time interval reaches a threshold of , The browser will clear the queue , One batch , In this way, you can turn multiple reflows and redraws into one .
When you access the following properties or methods , The browser will empty the queue immediately :
clientWidth、clientHeight、clientTop、clientLeft
offsetWidth、offsetHeight、offsetTop、offsetLeft
scrollWidth、scrollHeight、scrollTop、scrollLeft
width、height getComputedStyle()
getBoundingClientRect()
Because there may be operations in the queue that affect the return values of these properties or methods , Even if the information you want to get has nothing to do with the changes caused by the actions in the queue , The browser will also force the queue to clear , Make sure you get the most accurate value .
How to avoid
CSS
Avoid using table Layout .
As far as possible DOM The end of the tree changes class.
Avoid multiple inline styles .
Apply animation effects to position The attribute is absolute or fixed Elements on .
Avoid using CSS expression ( for example :calc()).
JavaScript
Avoid frequent operation styles , It's better to rewrite it once style attribute , Or define the style list as class And change it all at once class attribute .
Avoid frequent operation DOM, Create a documentFragment, Apply all... To it DOM operation , Finally add it to the document .
You can also set the element first display: none, Show it after the operation . Because in display The attribute is none On the elements of DOM Operation will not cause reflow and redraw .
Avoid backflow caused by frequent reads / Redrawn properties , If it does need to be used many times , Just cache it with a variable .
Use absolute positioning for elements with complex animation , Take it out of the document stream , Otherwise, it will cause frequent reflow of parent elements and subsequent elements .
边栏推荐
- [C language series] - constants and variables that confuse students
- 解决表单校验提示信息不消失问题以及赋值不生效问题
- 无重复字符的最长字串
- Do students in the science class really understand the future career planning?
- 第一周总结
- uniapp组件之选择选项(如套餐选择)
- Using POI TL to insert multiple pictures and the same data of multiple rows of cells into the table cells of word template at one time, it is a functional component for automatic merging
- Day 3
- Li Kou 994: rotten orange (BFS)
- uniapp之常用提示弹框
猜你喜欢
365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
·Let's introduce ourselves to the way of programming·
【TypeScript】深入学习TypeScript对象类型
ClickHouse学习(八)物化视图
ClickHouse学习(三)表引擎
Cmu15-213 shell lab experiment record
Clickhouse learning (VII) table query optimization
[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
Day 1
C language first level pointer
随机推荐
[electronic circuit] how to select ADC chip
js深拷贝-笔记
Clickhouse learning (VI) grammar optimization
微信小程序-屏幕高度
利用Poi-tl在word模板表格单元格内一次插入多张图片和多行单元格相同数据自动合并的功能组件
抽象类与接口
Hcia-r & s self use notes (26) PPP
Clickhouse learning (x) monitoring operation indicators
HCIA-R&S自用笔记(24)ACL
重绘与回流的关系
AR虚拟增强与现实
Clickhouse learning (V) cluster operation
Thrift安装手册
ClickHouse学习(五)集群操作
Detailed installation and use tutorial of MySQL (nanny installation with pictures and texts)
shell基本操作(下)
Clickhouse learning (IV) SQL operation
Allocate memory: malloc() and free()
H5语义化标签
uniapp之常用提示弹框