当前位置:网站首页>Redraw and reflow
Redraw and reflow
2022-07-03 04:13:00 【Chinese fir sauce_】
Rendering in the browser
- analysis HTML, formation DOM Trees , When encountering non blocking resources , For example, the image will continue to be parsed , When you meet css When you file , Will also continue to parse . When you meet js Blocking rendering of files , Will stop html Parsing .
- structure CSSOM Trees . The browser will css Rules are transformed into style maps that can be understood and used . Browser traversal CSS Each rule set in , according to CSS Selector creation has a parent 、 A node tree of child and sibling relationships .
- The third step is to DOM and CSSOM Combine into one Render Trees , Calculate the style or render tree from DOM The root of the tree begins to build , Traverse every visible node . Like and its child nodes and anything with
display:none
Nodes of styles , for examplescript {display:none;}
These labels will not show , That is, they will not appear in Render on the tree . havevisibility:hidden
The node will appear in Render on the tree , Because they take up space . Since we didn't give any instructions to override the user agent defaults , Therefore, in the above code example script Nodes will not be included in Render In the tree . - The fourth step is to run the layout on the rendering tree to calculate the geometry of each node .( backflow / rearrangement ). On this basis , Considering the size of the viewing area , The browser will determine the size of all the different boxes on the screen . Based on the size of the viewing area , Layout is usually from body Start , Arrange all with the model attributes of each element body The size of the descendant element , For replacement elements whose size is unknown ( Such as images ) Provide placeholder space .
- The last step is to draw the nodes on the screen .
backflow :
Based on the size of the viewing area , Layout is usually from body Start , Arrange all with the box model attribute of each element body The size of the descendant element , For replacement elements whose size is unknown ( Such as images ) Provide placeholder space .
All in all , Is to calculate them in the device viewport (viewport) The exact location and size of the inside ( That is, geometric attributes and positions ), The stage of calculation is reflux .
Repaint :
We can convert every node in the rendering tree into actual pixels on the screen , This stage is called the redraw node . That is to draw graphics on the screen , But if the background color changes , Then it will cause redrawing , But there will be no backflow .
Backflow must trigger a redraw , Redrawing doesn't necessarily trigger reflow .
Trigger reflow redrawing
- Add or remove visible DOM Elements
- The position of the element changes
- The size of the element changes ( Including margins 、 padding 、 The frame size 、 Height and width, etc )
- Content changes , Such as text changes or an image being replaced by another image of a different size .
- When the page starts to render ( It can't be avoided )
- The window size of the browser changes ( Because the backflow is based on the size of the viewport to calculate the location and size of the element )
Browser optimization mechanism
Each rearrangement results in additional computational overhead , Therefore, most browsers will optimize the rearrangement process through queue modification and batch execution . The browser queues up the changes , Until after a period of time or the operation reaches a certain threshold , Will clear the queue . however , When you get the operation of the layout information , A queue refresh is forced , Such as when you access the following properties or use the following methods :
- offsetTop、offsetLeft、offsetWidth、offsetHeight
- scroolTop、scroolLeft、scroolWidth、scroolHeight
- clientTop、clientLeft、clientWidth.clientHeight
- getComputedStyle()
- getBoundingClientRect
So when we modify the style , It is best to avoid using the properties listed above , They all refresh the render queue . If you want to use them , It is best to cache the values .
Ways to reduce reflow and redraw :
- The multiple DOM Operations are combined , Use csstext perhaps class
- Use cssText
const el = ducument.getElementById('test');
el.style.cssText += 'border-left: 1px;border-right: 2px; padding: 5px;';
- modify css Of class
const el = document.getElementById('test')
el.className += 'active';
- Make the node separate from the document flow ( That is, invisible nodes ), operation , Back to document flow .
- Use
display:none
- The layout event must be triggered
That is to say offset. If the same one is used many times offset, Then you can save its value before using . - For more complex animation, you can use absolute positioning to make it out of the document flow . For complex animation effects , Because it often causes reflow redrawing , therefore , We can use absolute positioning to separate it from the document flow , This causes frequent backflow of parent and subsequent elements .
边栏推荐
- Classes in TS
- 2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
- What is pytorch? Is pytorch a software?
- 阿洛对自己的思考
- [brush questions] most elements (super water king problem)
- [Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
- How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
- Write it down once Net travel management background CPU Explosion Analysis
- [fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
- pytorch是什么?pytorch是一个软件吗?
猜你喜欢
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
js实现在可视区内,文字图片动画效果
JS realizes lazy loading of pictures
JMeter starts from zero (III) -- simple use of regular expressions
How to download pytorch? Where can I download pytorch?
The 10th China Cloud Computing Conference · China Station: looking forward to the trend of science and technology in the next decade
China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects
MPLS setup experiment
国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来
Database management tool, querious direct download
随机推荐
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
[brush questions] most elements (super water king problem)
Interaction free shell programming
2022 tea master (primary) examination questions and tea master (primary) examination question bank
Leecode swipe questions and record LCP 18 breakfast combination
深潜Kotlin协程(十九):Flow 概述
sklearn数据预处理
Cnopendata China Customs Statistics
leetcode:297. Serialization and deserialization of binary tree
拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
Debug: CD cannot be used in kaggle
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
How does the pytorch project run?
[set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
Two points -leetcode-540 A single element in an ordered array
CVPR 2022 | Dalian Technology propose un cadre d'éclairage auto - étalonné pour l'amélioration de l'image de faible luminosité de la scène réelle
[set theory] inclusion exclusion principle (including examples of exclusion principle)
Application of I2C protocol of STM32F103 (read and write EEPROM)