当前位置:网站首页>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 .
边栏推荐
- paddle. Fluid constant calculation error 'nonetype' object has no attribute 'get_ fetch_ list‘
- Camunda 1、Camunda工作流-介绍
- Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
- JS simple code determines whether the device that opens the page is the PC end of the computer, the H5 end of the mobile phone, or the wechat end
- href与src的区别
- [C language series] - three methods to simulate the implementation of strlen library functions
- JS deep copy - Notes
- ClickHouse学习(一)ClickHouse?
- [sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
- 字符类型转换
猜你喜欢
![[C language series] - string + partial escape character explanation + annotation tips](/img/75/698ba0672af9d6118ee7e2fdf6daae.png)
[C language series] - string + partial escape character explanation + annotation tips

Clickhouse learning (IV) SQL operation

Basic concepts of MySQL + database system structure + extended application + basic command learning

Longest string without duplicate characters
![[C language series] - realize the exchange of two numbers without creating the third variable](/img/7c/468000bcbf740c9dd3535f2734728a.png)
[C language series] - realize the exchange of two numbers without creating the third variable

【C语言系列】— 一道递归小题目

Liang Yuqi, founder of aitalk: the link between image and virtual reality

Application of Huffman tree and Huffman coding in file compression

ClickHouse学习(六)语法优化

Day 2
随机推荐
365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13
uniapp组件之tab选项卡滑动切换
【电子电路】ADC芯片如何选型
【C语言系列】— 一道递归小题目
Detailed explanation of serial port communication
Day 3
ANSI C type qualifier
Playwright实战案例之爬取js加密数据
ClickHouse学习(十)监控运行指标
eggjs 创建应用知识点
ClickHouse学习(一)ClickHouse?
Detailed explanation of GPIO input and output
Clickhouse learning (IX) Clickhouse integrating MySQL
2022 mathematical modeling competition summer training lecture - optimization method: goal planning
力扣994:腐烂的橘子(BFS)
Basic use of redis
C language first level pointer
Flask 报错 RuntimeError: The session is unavailable because no secret key was set.
Wechat applet video upload component is directly uploaded to Alibaba cloud OSS
Clickhouse learning (V) cluster operation