Table of Contents
Written in front
Use as much hardware capabilities as possible (3D deformation will consume more memory and power consumption, so there should be Only use it when there is a performance problem, and it is a trade-off)
whenever possible Use less box-shadows and gradients
Keep animated elements out of the document flow as much as possible to reduce reflow
Optimize DOM layout performance
CSS properties behavior table in CSS animation
Home Web Front-end HTML Tutorial CSS3 Animation (Performance)_html/css_WEB-ITnose

CSS3 Animation (Performance)_html/css_WEB-ITnose

Jun 24, 2016 am 11:42 AM

Written in front

Compared with PC scenarios, high-performance mobile Web needs to consider more and more complex factors. We summarize them as follows: Traffic, Power consumption and fluency.

In the PC era, we pay more attention to the smoothness of the experience, but in the rich scenarios of the mobile side, we need to pay extra attention to the usage of user base station network traffic and device power consumption. situation.

Regarding fluency, it is mainly reflected in front-end animation. In the existing front-end animation system, there are usually two modes: JS animation and CSS3 animation. JS animation is a solution that uses JS to dynamically rewrite styles to achieve animation capabilities. It is a recommended solution for PCs that are compatible with low-end browsers. On the mobile side, we choose the native browser implementation with better performance: CSS3 animation.

However, CSS3 animations will face more performance problems than PCs in mobile multi-terminal device scenarios, mainly reflected in animation stuttering and flickering.

At present, there are several main methods to improve the mobile CSS3 animation experience:

Use as much hardware capabilities as possible (3D deformation will consume more memory and power consumption, so there should be Only use it when there is a performance problem, and it is a trade-off)

1. Use 3D deformation to turn on GPU acceleration

-webkit-transform: translate3d(0, 0, 0);-moz-transform: translate3d(0, 0, 0);-ms-transform: translate3d(0, 0, 0);transform: translate3d(0, 0, 0);
Copy after login

2. There is flickering during animation (usually occurs when the animation starts), you can try the following Hack

-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;
Copy after login

3. The animation smoothness of an element moved 500px to the right through translate3d will be significantly better than using the left attribute

  • CSS animation properties will trigger the entire page to reflow layout, repaint, and reorganize recomposite
  • Paint is usually the most expensive among them. Try to avoid using CSS animation properties that trigger paint. This is also Why do we recommend using webkit-transform: translateX(3em) in CSS animations instead of using left: 3em, because left will additionally trigger layout and paint, while webkit-transform only triggers the entire page composite (this is also why it is recommended in CSS animations Use the solution of webkit-transform: translateX(500px) instead of using left: 500px)
  • #ball-1 {  transition: -webkit-transform .5s ease;  -webkit-transform: translate3d(0, 0, 0);}#ball-1.slidein {  -webkit-transform: translate3d(500px, 0, 0);}#ball-2 {  transition: left .5s ease;  left: 0;}#ball-2.slidein {  left: 500px;}
    Copy after login

    div {  -webkit-animation-duration: 5s;  -webkit-animation-name: move;  -webkit-animation-iteration-count: infinite;  -webkit-animation-direction: alternate;  width: 200px;  height: 200px;  margin: 100px;  background-color: #808080;  position: absolute;}/*第一种方案  用css属性left*/@-webkit-keyframes move{    from {        left: 100px;    }    to {        left: 200px;    }}/*第二种方案  用css3动画属性translateX*/@-webkit-keyframes move{    from {        -webkit-transform: translateX(100px);    }    to {        -webkit-transform: translateX(200px);    }}
    Copy after login

    whenever possible Use less box-shadows and gradients

    Box-shadows and gradients are often performance killers of the page, especially when they are used in one element at the same time, so embrace flat design

    Keep animated elements out of the document flow as much as possible to reduce reflow

    position: fixed;position: absolute;
    Copy after login

    Optimize DOM layout performance

    // 触发两次 layoutvar newWidth = aDiv.offsetWidth + 10;   // ReadaDiv.style.width = newWidth + 'px';     // Writevar newHeight = aDiv.offsetHeight + 10; // ReadaDiv.style.height = newHeight + 'px';   // Write// 只触发一次 layoutvar newWidth = aDiv.offsetWidth + 10;   // Readvar newHeight = aDiv.offsetHeight + 10; // ReadaDiv.style.width = newWidth + 'px';     // WriteaDiv.style.height = newHeight + 'px';   // Write
    Copy after login

    Continuously reading the offsetWidth/Height properties and continuously setting the width/height properties can trigger the layout one less time than reading and setting individual properties separately. From the conclusion, it seems to be related to the execution queue. Yes, this is the optimization strategy of the browser. All operations that can trigger layout will be temporarily put into the layout-queue. When it must be updated, the results of all operations in the entire queue will be calculated , so that you can only One-time layout to improve performance.

    Under what operations will the layout be updated (also called reflow or relayout)?

  • Element: clientHeight, clientLeft, clientTop, clientWidth, focus(), getBoundingClientRect(), getClientRects(), innerText, offsetHeight, offsetLeft, offsetParent, offsetTop, offsetWidth, outerText , scrollByLines(), scrollByPages(), scrollHeight, scrollIntoView(), scrollIntoViewIfNeeded(), scrollLeft, scrollTop, scrollWidth

  • Frame, HTMLImageElement: height, width

  • Range: getBoundingClientRect(), getClientRects()

  • SVGLocatable: computeCTM(), getBBox()

  • SVGTextContent: getCharNumAtPosition(), getComputedTextLength(), getEndPositionOfChar(), getExtentOfChar(), getNumberOfChars(), getRotationOfChar(), getStartPositionOfChar() , getSubStringLength(), selectSubString()

  • SVGUse: instanceRoot

  • window: getComputedStyle (), scrollBy(), scrollTo(), scrollX, scrollY, webkitConvertPointFromNodeToPage(), webkitConvertPointFromPageToNode()

  • CSS properties behavior table in CSS animation

    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

    Video Face Swap

    Video Face Swap

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

    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)

    Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    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.

    Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

    What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

    AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

    Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

    GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

    How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

    The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

    HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

    HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

    How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

    To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

    See all articles