We know that browsers go through the following steps when parsing a document:
- will
HTML
Resolve toDOM
That will beCSS
Resolve toCSSOM
.DOM
andCSSOM
Combined to generateRender Tree
- Then according to the
Render Tree
Draw nodes on the page
Backflow is the process by which the browser rerenders part or all of the document when the size, structure, or properties of an element change. Redraw is when the style of an element changes without changing its position in the document flow.
Reflux (Reflow)
Now that we understand backflow, let’s look at what actions can cause browser backflow:
- The browser window size changed. Procedure
- The size or position of the element changed
- Element content changes
- Element font changes
- Add or remove
DOM
- The activation
CSS
pseudo-classes - Query some properties or call some methods:
ClientWidth, clientHeight, clientTop, clientLeft
OffsetWidth, offsetHeight, offsetTop, offsetLeft
ScrollWidth, scrollHeight, scrollTop, scrollLeft
ScrollIntoView (), scrollIntoViewIfNeeded ()
getComputedStyle()
getBoundingClientRect()
scrollTo()
Redraw (Repaint)
When the style change of an element on the page does not affect its position in the document flow, the browser simply assigns the new style to it and redraws it, which is called redraw. For example, color, background-color, and visibility
thinking
So how do we avoid redrawing and refluxing?
- Avoid the use of
table
layout - Apply the animation effect to
position
Properties forabsolute
orfixed
Elements that leave the document flow do not cause backflow - You can use
tranform
Property to set the animation - Avoid frequent manipulation styles and use them instead
class
To change the style - Avoid frequent operations
DOM
, create adocumentFragment
It’s on top of itDOM
operation - You can start by setting the element to
display:none
, and then display it after operation. Because in thedisplay
fornone
Operation on the element does not cause redraw and reflow