For Web page rendering, it's something that every front-end developer should know!

Source: Internet
Author: User
Tags border color css preprocessor

Web page rendering must be done at an early stage, as early as the page layout has just been finalized. Because styles and scripts can have a critical impact on Web page rendering. Therefore, professional developers must understand some of the techniques to avoid encountering performance problems in the process of practice.

This article does not look at the detailed mechanisms inside the browser, but rather presents some common rules. After all, different browser engines work differently, which will undoubtedly make it more complicated for developers to study browser features.

How does a browser complete Web page rendering?

First, let's review the browser's actions when the page is rendered:

form the Document Object Model (DOM) based on the HTML code from the server side

Load and parse styles to form a CSS object model.

On top of the Document Object model and CSS object model, create a render tree that consists of a set of objects to be rendered (in WebKit, these objects are referred to as renderers or render objects, and are called "frame" in gecko. The render tree reflects the structure of the Document object model, but does not include invisible elements such as labels or containing display:none properties. In the render tree, each piece of text string behaves as a separate renderer. Each Render object contains a corresponding Dom object, or block of text, plus a computed style. In other words, a rendering tree is a visual representation of a Document object model.

For each element on the render tree, calculate its coordinates, called the layout. The browser takes a stream method, layout an element only once, but the table element needs to pass multiple times.

Finally, the elements on the render tree are finally displayed in the browser, a process called "painting".

When the user interacts with the Web page, or when the script changes the page, some of the actions mentioned earlier will be repeated, as the intrinsic structure of the page has changed.

Redraw

When changing the element style that does not affect the position of the element in the page, such as background-color (background color), Border-color (border color), visibility (visibility), the browser will only redraw the element once with the new style (this is redrawing, or re-structuring the style).

Re-arrange

Reflow or re-layout occurs when the change affects the text content or structure, or the position of the element. These changes are usually triggered by the following events:

Dom manipulation (element additions, deletions, modifications, or changes in the order of elements);

Changes in content, including changes in text within a form field;

The calculation or change of CSS properties;

Add or remove style sheets;

Change the properties of the class;

Operation of the browser window (zoom, scroll);

Pseudo-Class activation (: hover).

How does the browser optimize rendering?

As much as possible, the browser restricts redrawing/refactoring to the area of the element being changed. For example, for a fixed or absolute position, the size change affects only the element itself and its child elements, however, the change in the size of the statically positioned element triggers the heavy flow of all subsequent elements.

Another optimization technique is that when you run a few pieces of JavaScript code, the browser caches the changes and then applies the changes once the code has finished running. For example, the following code will only trigger a refactoring and redraw:

var $body = $ (' body ');

$body. css (' padding ', ' 1px '); Reflow, repaint

$body. CSS (' Color ', ' red '); Repaint

$body. CSS (' margin ', ' 2px '); Reflow, repaint

Only 1 reflow and repaint would actually happen

However, as mentioned earlier, changing the attributes of an element triggers a mandatory rearrangement. This behavior can occur if we include a line of code in the block above to access the attributes of the element.

var $body = $ (' body '); $body. css (' padding ', ' 1px '); $body. css (' padding '); Reading a property, a forced reflow $body. CSS (' Color ', ' red '); $body. CSS (' margin ', ' 2px ');

The result is that the rearrangement occurred two times. Therefore, you should organize the operations that access element properties together to optimize Web page performance. (You can find more detailed examples in Jsbin.)

Sometimes, you have to trigger a mandatory reflow. For example, we must assign the same attributes (such as the left margin) two times to the same element. At first, it should be set to 100px, and it does not drive the effect. Next, it must be changed to 50px by the Transition (transition) dynamic effect. You can now learn this example on Jsbin, but I'll introduce it in more detail here.

First, we create a CSS class with transition effects:. has-transition {-webkit-transition:margin-left 1s ease-out;-moz-transition:margin-left 1s Ease-out; -o-transition:margin-left 1s ease-out; Transition:margin-left 1s ease-out; }

Then continue execution://Our element is a "has-transition" class by default var $targetElem = $ (' #targetElemId ');

Remove the Transition class

$targetElem. Removeclass (' has-transition ');

The change of the property expecting the transition to being off, as the class is not there

Anymore

$targetElem. CSS (' margin-left ', 100);

Put the transition class back

$targetElem. addclass (' has-transition ');

Change of the property

$targetElem. CSS (' Margin-left ', 50);

However, this implementation is not effective. All changes are cached and executed only at the end of the code block. What we need is a mandatory rearrangement, which we can implement with the following changes:

Remove the Transition class $ (this). Removeclass (' has-transition ');

Change of the property

$ (this). CSS (' margin-left ', 100);

Trigger a forced reflow, so this changes in a Class/property get applied immediately

$ (this) [0].offsetheight; An example, and other properties would work, too

Put the transition class back

$ (this). addclass (' has-transition ');

Change of the property

$ (this). CSS (' Margin-left ', 50);

The code now executes as expected.

Practical recommendations for performance tuning

Summing up the existing information, I make the following recommendations:

Create a valid HTML and CSS file, and don't forget to specify how the document is encoded. The style should be contained within the tag, and the script code should be added to the end of the tag.

Try to simplify and optimize the CSS selector (this optimization is almost uniformly ignored by developers using CSS preprocessor) to keep the nesting level to a minimum. The following is the performance ranking of the CSS selector (starting with the fastest person)

1. Recognizer: #id

2. Classes:. Class

3. Tags: div

4. Adjacent Sibling selector: A + I

5. Parent class selector:ul> Li

6. Universal selector: *

7. Attribute selection: input[type= "text"]

8. Pseudo-class and pseudo-elements: a:hover

You should keep in mind that the browser handles selectors in the right-to-left principle, so the right-most selector should be the fastest: #id或则. Class:div * {...}//bad. List Li {...}//bad. List-item {...}//Good #l ist. List-item {...}//good * 1. In your scripting code, minimize DOM operations. Caches everything, including element attributes and objects (if they are reused). When complex operations are performed, it is better to use the "orphaned" element, which can then be added to the DOM (the so-called "orphaned" element is detached from the DOM and stored only in memory).

2. If you use jquery to select elements, follow the jquery selector best practices scenario.

3. In order to change the style of an element, modifying the properties of the "class" is one of the methods that works. When this change is performed, the darker the DOM rendering tree is, the better (which also helps to detach the logic from the façade).

4. Try to animate only the absolute or fixed elements of the position.

5. Disable complex hover action when using scrolling (for example, add an extra non-hover class in).

http://biyinjishi.blog.163.com/blog/static/2591020362016320104758944/
Http://biyinjishi.lofter.com/post/1de35feb_ab02eef
http://blog.tianya.cn/post-7182283-114564619-1.shtml
http://blog.tianya.cn/post-7182283-114564880-1.shtml
http://blog.sina.com.cn/s/blog_15e2eae990102waq2.html
http://blog.sina.com.cn/s/blog_15e2eae990102waq5.html
http://blog.tianya.cn/post-7199537-114565603-1.shtml
http://blog.tianya.cn/post-7199537-114565715-1.shtml
http://blog.tianya.cn/post-7199537-114565804-1.shtml
http://blog.tianya.cn/post-7199537-114566024-1.shtml
http://blog.tianya.cn/post-7199537-114566082-1.shtml
http://blog.tianya.cn/post-7199537-114566174-1.shtml
http://blog.tianya.cn/post-7199537-114566280-1.shtml
http://blog.tianya.cn/post-7199537-114566401-1.shtml
http://blog.tianya.cn/post-7199537-114566507-1.shtml
http://blog.tianya.cn/post-7199537-114566562-1.shtml
http://www.cnblogs.com/SA-Jim/p/5411880.html
http://www.cnblogs.com/SA-Jim/p/5411897.html
http://www.cnblogs.com/SA-Jim/p/5411903.html
http://www.cnblogs.com/SA-Jim/p/5411909.html
http://www.cnblogs.com/SA-Jim/p/5411918.html
http://www.cnblogs.com/SA-Jim/p/5411926.html
http://www.cnblogs.com/SA-Jim/p/5411933.html
http://www.cnblogs.com/SA-Jim/p/5411936.html
http://www.cnblogs.com/SA-Jim/p/5411945.html
http://tieba.baidu.com/p/4492693741
http://baa.bitauto.com/bj/thread-9020277.html#131720055
http://bbs.pcauto.com.cn/topic-10986168.html
http://bbs.pcauto.com.cn/topic-10986216.html
http://baa.bitauto.com/bj/thread-9020373.html#131721132
http://www.biyinjishi.com/kdoc/89456305/
http://www.biyinjishi.com/kdoc/89456308/
http://www.biyinjishi.com/kdoc/89456299/
http://www.biyinjishi.com/kdoc/89456297/
http://www.biyinjishi.com/kdoc/89456300/
http://www.biyinjishi.com/kdoc/89456303/
http://www.biyinjishi.com/kdoc/89456306/
http://www.biyinjishi.com/kdoc/89456296/
http://www.biyinjishi.com/kdoc/89456310/
http://www.biyinjishi.com/kdoc/89456298/
http://www.biyinjishi.com/kdoc/89456301/
http://www.biyinjishi.com/kdoc/89456302/
http://www.biyinjishi.com/kdoc/89456304/
http://www.biyinjishi.com/kdoc/89456307/
http://www.biyinjishi.com/kdoc/89456309/

For Web page rendering, it's something that every front-end developer should know!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.