Home > Article > Web Front-end > Reflows and Repaints in Javascript
A reflow (also called layout or re-layout) occurs when the browser recalculates the position, size, and layout of elements on the page. This process happens every time the layout of the page changes such as when elements are added, removed, resized, or their visibility changes. It’s a more complex and time-consuming operation
<div id="box" style="width: 100px; height: 100px; background-color: blue;"></div> <script> const box = document.getElementById('box'); // Triggering a reflow by changing width and height box.style.width = '200px'; box.style.height = '200px'; // Triggering a repaint by changing the background color box.style.backgroundColor = 'red'; </script>
When you change something that affects the layout of the page, the browser has to:
If many elements are affected by a single change, the reflow can be costly and slow down the performance of your site.
A repaint (or redraw) occurs when visual properties of an element change but the layout doesn’t. It’s less expensive than a reflow because it only requires updating the appearance of elements without recalculating their position or layout. Repaints happen after the layout is recalculated (in cases where both are needed) or when non-layout-affecting properties are changed, like color or visibility.
<div id="box" style="width: 100px; height: 100px; background-color: blue;"></div> <script> // Triggering a repaint by changing the background color box.style.backgroundColor = 'red'; </script>
A repaint doesn’t involve recalculating layout, so it’s faster than reflow, but it still requires redrawing parts of the page, which takes some time.
Minimize DOM manipulation : Use techniques like batching DOM updates (as mentioned earlier) or DocumentFragment to make multiple changes at once, instead of one by one.
Avoid layout thrashing : If you read a layout property (e.g., offsetHeight) and immediately write (change the layout) in the same cycle, it forces a reflow, known as layout thrashing. To avoid this, separate reading and writing DOM properties in different steps.
<div id="box" style="width: 100px; height: 100px; background-color: blue;"></div> <script> const box = document.getElementById('box'); // Triggering a reflow by changing width and height box.style.width = '200px'; box.style.height = '200px'; // Triggering a repaint by changing the background color box.style.backgroundColor = 'red'; </script>
Use CSS classes : Instead of modifying individual styles, use CSS classes to make changes. The browser handles class switching more efficiently.
<div id="box" style="width: 100px; height: 100px; background-color: blue;"></div> <script> // Triggering a repaint by changing the background color box.style.backgroundColor = 'red'; </script>
Reduce complexity of CSS: Avoid deeply nested elements and overly complex CSS rules that can trigger reflows.
Use visibility: hidden instead of display: none when you just want to hide an element without affecting layout. display: none triggers a reflow, while visibility: hidden only triggers a repaint.
The above is the detailed content of Reflows and Repaints in Javascript. For more information, please follow other related articles on the PHP Chinese website!