search
HomeWeb Front-endCSS TutorialExamples of css redrawing and rearrangement

This article mainly introduces to you the relevant information about CSS redrawing and reflowing methods. The editor thinks it is quite good, so I will share it with you now, hoping to help everyone.

Principle of browser loading page

Usually when the document is first loaded, the browser engine will parse the HTML document to build a DOM tree, and then build it based on the geometric attributes of the DOM element A tree for rendering. Each node of the rendering tree has attributes such as size and margin, similar to the box model (since hidden elements do not need to be displayed, the rendering tree does not contain hidden elements in the DOM tree). When the rendering tree is constructed, the browser can place the elements in the correct position, and then draw the page based on the style attributes of the rendering tree nodes. Due to the browser's flow layout, the calculation of the rendering tree usually only needs to be traversed once. With the exception of table and its internal elements, it may require multiple calculations to determine the attributes of its nodes in the rendering tree, which usually takes 3 times the time of equivalent elements. This is one reason why we should avoid using tables for layout.

Redraw

Redraw is a browser behavior triggered by a change in the appearance of an element, such as changing attributes such as visibility, outline, and background color. The browser will redraw the element based on its new attributes, giving the element a new appearance. Redrawing does not bring about re-layout and is not necessarily accompanied by reflow. Browsers pay a high performance price when redrawing and reflowing.

Rearrangement

Rearrangement is a more obvious change, which can be understood as the rendering tree needs to be recalculated. The following are common operations that trigger reordering:

  1. Geometry attribute changes of DOM elements.

  2. Structural changes of the DOM tree.

For example, the addition, subtraction, movement of nodes, etc.

Get some attributes.

When getting some attributes, the browser will also trigger reflow to get the correct value. This renders the browser's optimization ineffective. These properties include: offsetTop, offsetLeft, offsetWidth, offsetHeight, scrollTop, scrollLeft, scrollWidth, scrollHeight, clientTop, clientLeft, clientWidth, clientHeight, getComputedStyle() (currentStyle in IE). Therefore, these values ​​should be cached when used multiple times.

In addition, changing some styles of elements, resizing the browser window, appearing scroll bars, etc. will also trigger reflow.

Reduce the number of rearrangements and the scope of impact of rearrangements

1. Combine multiple operations of changing style attributes into one operation. For example,


JS:
    var changep = document.getElementById(‘changep’);
    changep.style.color = ‘#093′;
    changep.style.background = ‘#eee';
    changep.style.height = ‘200px';
    可以合并为:
CSS:
    p.changep {
        background: #eee;
        color: #093;
        height: 200px;
    }
JS:
    document.getElementById(‘changep’).className = ‘changep';

2. Set the position attribute of an element that needs to be rearranged multiple times to absolute or fixed, so that the element is out of the document flow and its changes will not change. will affect other elements. For example, elements with animated effects are best set to absolute positioning.

3. Operate the node multiple times in the memory, and then add it to the document after completion. For example, you want to obtain table data asynchronously and render it to the page. You can first obtain the data and then build the html fragment of the entire table in memory, and then add it to the document at once, instead of adding each row in a loop.

4. Since elements with a display attribute of none are not in the rendering tree, operations on hidden elements will not cause the rearrangement of other elements. If you want to perform complex operations on an element, you can hide it first and then display it after the operation is completed. This only triggers 2 reflows when hiding and showing.

5. When you need to frequently retrieve attribute values ​​that cause browser reflow, you need to cache them in variables

Related recommendations:

Page reflow Optimization methods for drawing and reflow

High-performance WEB development page rendering, redrawing, and reflow.

js generates thumbnails and then uploads them and uses canvas to redraw_javascript skills

The above is the detailed content of Examples of css redrawing and rearrangement. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.