What Are Browser Repaint and Reflow
Definition of Repaint and Reflow
Repaint: When the style of the current element (background color, font color, etc.) changes, we only need to re-render the changed element. Repaint has less impact on browser performance, so it’s generally not considered. Situations that cause repaint: changing container appearance styles, such as background: black, etc. Changing appearance without changing layout, not affecting other DOM elements.
Reflow: The process by which the browser recalculates the position and geometric structure of elements in the document to re-render part or all of the document. Because reflow may cause the entire DOM tree to be reconstructed, it’s a major performance killer. A reflow of one element causes subsequent reflows of all its child elements and ancestor elements that follow it in the DOM.
Browser Rendering Process
Rendering: The process by which the browser displays HTML code according to CSS-defined rules in the browser window
Basic Process of Browser Parsing HTML:
User enters URL, browser sends request to server, server returns HTML file
Browser loads HTML code, finds a tag referencing an external CSS file
Browser sends request for CSS file, server returns this CSS file
Browser continues loading part of the HTML code, and the CSS file is already obtained, can render the page
Browser finds a tag referencing an image in the code, sends request to server. At this point, the browser won’t wait for the image to download, but continues rendering the following code
Server returns image file, since the image occupies a certain area, affecting the layout of subsequent paragraphs, the browser needs to go back and render this part of the code
Browser finds a script tag containing a line of JavaScript code, quickly runs it
JavaScript script executes this statement, commands browser to hide a certain element in the code, tragedy strikes, suddenly one element is missing, browser has to re-render this part of the code
Finally the closing tag arrives, browser tears streaming down
Wait, not done yet, user clicks a “change theme” button in the interface, JavaScript makes browser change the CSS path in a tag
Browser gathers everyone present: “Folks need to pack up, we have to start over”, browser requests new CSS file from server, re-renders the page. When page layout changes, browser goes back to re-render, this is why pages become slow.
Operations That Trigger Reflow
Resizing the window
Changing the font
Adding or removing stylesheets
Content changes, such as user typing text in an input box
Activating CSS pseudo-classes, such as :hover (in IE, activation of sibling pseudo-classes)
Manipulating the class attribute
Script manipulating the DOM
Calculating offsetWidth and offsetHeight properties
Setting values of style attribute properties
Elements with fixed positioning will continuously reflow when dragging the scrollbar
References:
http://blog.sina.com.cn/s/blog_8dace7290102wezv.html
http://www.cnblogs.com/jyybeam/p/5776667.html