当前位置:网站首页>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 .
边栏推荐
- Clickhouse learning (IX) Clickhouse integrating MySQL
- 2022 mathematical modeling competition summer training lecture - optimization method: goal planning
- B - 识别浮点常量问题
- Provincial and urban three-level linkage (simple and perfect)
- [electronic circuit] how to select ADC chip
- Hcia-r & s self use notes (24) ACL
- Dynamic sorting of DOM object element blocks in applets
- MySQL解压版windows安装
- One dimensional array exercise
- uniapp组件之tab选项卡滑动切换
猜你喜欢

HCIA-R&S自用笔记(26)PPP

MySQL解压版windows安装

Li Kou 994: rotten orange (BFS)

eggjs 创建应用知识点

【TypeScript】深入学习TypeScript函数

PyQt5:第一章第1节:使用Qt组件创建一个用户界面-介绍

Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"

微信小程序-屏幕高度

Global components component registration

C language first level pointer
随机推荐
Three handshakes and four waves for the interview summary
Merge the same items in the same column in table
[electronic circuit] how to select ADC chip
【无标题】
Clickhouse learning (VIII) materialized view
eggjs 创建应用知识点
公众号不支持markdown格式文件编写怎么办?
ClickHouse学习(十)监控运行指标
移动端-flex项目属性
Detailed installation and use tutorial of MySQL (nanny installation with pictures and texts)
B - identify floating point constant problems
[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
TXT 纯文本操作
ClickHouse学习(五)集群操作
Day 5
HCIA-R&S自用笔记(24)ACL
使用微信小程序扫码登录系统PC端web的功能
抽象类与接口
Flask 报错 RuntimeError: The session is unavailable because no secret key was set.
uniapp组件之tab选项卡滑动切换