Table of Contents
Previous words
Preparatory knowledge
Syntax
Don’t be like Write the following directly in the default state, because will-change will always be hanging:
  2. There are Use sparingly: Typically, browsers discard optimization work when an element is restored to its original state. However, if the will-change attribute is explicitly declared directly in the style sheet, it means that the target element may change frequently, and the browser will save the optimization work longer than before. So the best practice is to switch the value of will-change through script before and after the element changes
Home Web Front-end CSS Tutorial CSS page rendering optimization property will-change

CSS page rendering optimization property will-change

Feb 20, 2017 am 11:37 AM

Previous words

 When we trigger the page to draw a large area through certain behaviors (click, move or scroll), the browser is often unprepared and can only passively use the CPU to calculate and re-draw the page. Drawing, because there was no preparation in advance, it was difficult to deal with rendering, so the frame dropped and stuck. The CSS attribute will-change provides web developers with a way to tell the browser what changes will occur to the element, so that the browser can make corresponding optimization preparations in advance before the element attributes actually change. This kind of optimization can prepare part of the complex calculation work in advance, making the page response faster and more sensitive. This article will introduce the CSS property will-change

Preparatory knowledge

 GPU is a graphics processor that specializes in processing and drawing graphics-related hardware. The GPU is specially designed to perform complex mathematical and geometric calculations, freeing the CPU from graphics processing tasks and able to perform other more system tasks

 The so-called hardware acceleration means computing in the computer A very large amount of work is allocated to specialized hardware for processing, reducing the workload of the CPU

  CSS animations, transformations, and gradients do not automatically trigger GPU acceleration, but use the browser's slightly slower software rendering engine . In the world of transition, transform and animation, the process should be offloaded to the GPU to speed things up. Only 3D deformation will have its own layer, while 2D deformation will not.

【Hack】

Use translateZ() or translate3d() The method adds an unchanged 3D deformation to the element and tricks the browser into triggering hardware acceleration. However, the cost is that this situation takes up RAM and GPU storage space by overlaying elements to its own layer, and the space release time cannot be determined

Syntax

will-change

 Function: Notify the browser in advance of what animation the element will do, allowing the browser to prepare appropriate optimization settings in advance

 Value: auto | &lt ;animateable-feature>

Initial value: auto

Applies to: all elements

Inheritance: None

Compatibility: IE13+, chrome49+, safari9.1+, IOS9.3+, Android52+

  auto means that there is no specific specification of which attributes will change. The browser needs to guess by itself, and then use some conventional methods commonly used by browsers to optimize

  <animateable-feature> can be the following values: animate

÷ contents Indicates that the developer wants to change something in the content of the element in the near future, or animate them

÷ <custom-ident&gt ; indicates that the developer hopes to change the specified property name or animate it in the near future. If the attribute name is an abbreviation, it represents all the corresponding abbreviations or full-length attributes

Use

【Use hover】

Don’t be like Write the following directly in the default state, because will-change will always be hanging:

.will-change {
  will-change: transform;
  transition: transform 0.3s;
}
.will-change:hover {
  transform: scale(1.5);
}
Copy after login

You can declare will-change when the parent element hovers, so that it will automatically remove when it is moved out, which is triggered. The scope is basically the valid element range

.will-change-parent:hover .will-change {
  will-change: transform;
}
.will-change {
  transition: transform 0.3s;
}
.will-change:hover {
  transform: scale(1.5);
}
Copy after login

[Use javascript script]

.sidebar {
  will-change: transform;
}
Copy after login

 The above example directly adds the will-change attribute to the style sheet, which will cause the browser to continue the corresponding optimization work. Saved in memory, this is actually unnecessary. The following shows how to use a script to correctly apply the

will-change

attribute

var el = document.getElementById('element');
// 当鼠标移动到该元素上时给该元素设置 will-change 属性
el.addEventListener('mouseenter', hintBrowser);
// 当 CSS 动画结束后清除 will-change 属性
el.addEventListener('animationEnd', removeHint);
function hintBrowser() {
  // 填写在CSS动画中发生改变的CSS属性名
  this.style.willChange = 'transform, opacity';
}
function removeHint() {
  this.style.willChange = 'auto';
}
Copy after login

[Direct use] However, if an application turns pages when the keyboard is pressed , such as photo albums or slideshows, the pages are large and complex. At this time, it is appropriate to write will-change in the style sheet. This will make the browser prepare the transition animation in advance, and you can see the flexible and brisk animation when the keyboard is pressed

.slide {
  will-change: transform;
}
Copy after login

Notes

1. Don’t Applying will-change to too many elements: The browser is already trying to optimize everything it can. There are some more powerful optimizations that, if combined with will-change, may consume a lot of machine resources. If used excessively, it may cause the page to respond slowly or consume a lot of resources.

  2. There are Use sparingly: Typically, browsers discard optimization work when an element is restored to its original state. However, if the will-change attribute is explicitly declared directly in the style sheet, it means that the target element may change frequently, and the browser will save the optimization work longer than before. So the best practice is to switch the value of will-change through script before and after the element changes

  3. Don’t apply will-change optimization prematurely: If the page has no performance problems, don’t add the will-change attribute to squeeze out a little bit of speed. The original design intention of will-change is as a last resort optimization method to try to solve existing performance problems. It should not be used to prevent performance problems. Excessive use of will-change can lead to large memory usage and a more complex rendering process as the browser attempts to prepare for possible changes. This will lead to more serious performance problems

 4. Give it enough working time: This attribute is used to allow page developers to inform the browser which attributes may change. The browser can then choose to do some optimization work in advance before the change occurs. So it's very important to give the browser some time to actually do these optimizations. When using it, you need to try to find some ways to know the possible changes of the element at a certain time in advance, and then add the will-change attribute to it

For more CSS page rendering optimization attributes will-change, please pay attention to the PHP Chinese website for related articles !

Statement of this Website
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

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

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

See all articles