CSS animation performance optimization
1. Use css, jquery, canvas to create animations
1. Canvas
Advantages: good performance, powerful, supports most browsers (except IE6, IE7, IE8), the drawn graphics can be directly saved as .png or .jpg graphics;
Disadvantages: Dependent on HTML, graphics can only be drawn through scripts, and there is no API to implement animation (relying on events) and timer updates); since text displayed programmatically on canvas is actually a bitmap, search crawlers will ignore the text entirely. Text content is also not recognized by screen readers.
2, css3
Advantages: simple and separated from content, css animation does not trigger layout and paint; (modification of these properties will not trigger layout and paint: backface-visibility, opacity, perspective , perspective-origin, transform);
Disadvantages: There are browser compatibility issues, Android phones may experience lag, and are restricted by the typesetting engine, which is closely related to the DOM structure of the entire page.
3. JQuery
Advantages: No compatibility issues
Disadvantages: repaint and recomposite are required for each frame (very time-consuming);
Summary: In terms of mobile animation effects, using css3 animation is much more efficient than jquery animation. The performance is especially obvious on Android phones! Therefore, mobile animations prioritize CSS3 animations, and jquery can only be used to simply handle application logic. CSS3 animation is a general solution for adding special effects to content layout, but on mobile browsers with poor performance, it is likely to be limited by the layout performance and cannot achieve the desired effect. For specific scenarios that require performance, such as games, canvas will be greatly improved.
(Recommended tutorial: CSS Getting Started Tutorial)
2. CSS3 freezes on the mobile terminal
The animation produced by css3 runs on ios 66, but sometimes lags occur on Android. You might as well look for problems from the following points.
a. Whether it causes layout
If so, make the animation elements absolute or fixed as much as possible to avoid affecting the document tree and reduce reflow.
b. Whether it is enabled Hardware acceleration
"Using CSS3 animation" and "turning on hardware acceleration" are two different things, although the former may cause the latter.
There is a magical panacea for turning on hardware acceleration in webkit: opacity: 1; or -webkit-backface-visibility: hidden;.
c. Whether there are high-cost attributes (css shadow, gradients, background-attachment: fixed, etc.)
If so, pictures are also an option. This can be regarded as an optimization that trades space for time.
d. Area of repaint
If so, we have to reduce the animation area. The optimization of this step is limited;
e. Try to use transform to generate animation and avoid using height, width, margin, padding, etc.; such as the following examples 1 and 2.
PS: Using transform, the browser only needs to generate the bitmap of this element once and submit it to the GPU for processing when the animation starts. After that, the browser does not need to do any layout, drawing, and submission of bitmap operations. As a result, the browser can take full advantage of the GPU's capabilities to quickly draw bitmaps in different positions, perform rotation or scaling. In short, the transform animation is controlled by the GPU, supports hardware acceleration, and does not require software rendering
3. There is flickering during the animation process (usually occurs at the beginning of the animation)
Solution:
.cube { -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -ms-backface-visibility: hidden; backface-visibility: hidden; -webkit-perspective: 1000; -moz-perspective: 1000; -ms-perspective: 1000; perspective: 1000; /* Other transform properties here */ }
In browsers with webkit kernel, another effective method is:
.cube { -webkit-transform: translate3d(0, 0, 0); -moz-transform: translate3d(0, 0, 0); -ms-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); /* Other transform properties here */ }
Recommended related video tutorials: css video tutorial
The above is the detailed content of CSS animation performance optimization. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
