JS animation implementation method
There are six main ways to implement animation: Javascript directly implements animation, scalable vector graphics (SVG) animation, CSS transition, CSS3 animation, Canvas animation, and requestAnimationFrame.
javascript implementation
<!DOCTYPE html><html><head> <style> .content{width: 100px;height: 100px;background-color: red;} </style></head><body><p class="content"></p><script> var ele=document.getElementsByClassName("content")[0]; var left=0; let timer=setInterval(function () { if(left<window.innerWidth-100){ ele.style.marginLeft=left+'px'; left++; } else { clearInterval(timer); } },16);</script></body></html>
Disadvantages: Implementing animation through Javascript usually results in frequent page re-arrangement and redrawing, which consumes performance.
According to common sense, changing the position of elements will cause rearrangement. Why are all the images shown in the above picture redrawn? The reason is that absolute positioning will create a new layer, and there is only the current element on this layer, so it will only be redrawn, not rearranged. This also tells us that in the same layer, when the number of elements is small, the reordering performance is better and the speed will be faster.
CSS3 transition
<!DOCTYPE html><html><head> <style> .content{ width: 100px; height: 100px; background-color: red; transition: all 10s ease-in-out 0s; -webkit-transition: all 10s ease-in-out 0s; margin-left: 0; } .right{ width: 100px; height: 100px; margin-left: 400px; background-color: blue; } </style></head><body><p class="content"></p><script>var timer=setTimeout(function () { var content=document.getElementsByClassName("content")[0]; content.setAttribute('class','right'); },500);</script></body></html>
Why does transform not trigger repaint? In short, transform animation is controlled by the GPU, supports hardware acceleration, and does not require software rendering.
Hardware Acceleration Principle
After the browser receives the page document, it will parse the markup language in the document into a DOM tree. The DOM tree and CSS are combined to form the rendering tree for the browser to build the page. The rendering tree contains a large number of rendering elements. Each rendering element will be divided into a layer, and each layer will be loaded into the GPU to form a rendering texture. The layer transform in the GPU will not trigger repaint. , eventually these layers using transform will be processed by independent compositor processes.
CSS3 animation
<!DOCTYPE html><html><head> <style> .content{ width: 100px; height: 100px; background-color: red; transition: all 10s ease-in-out 0s; -webkit-transition: all 10s ease-in-out 0s; animation: move 4s infinite; margin-left: 0; } @keyframes move { from{ margin-left: 0; } 50%{ margin-left: 400px; } to{ margin-left: 0; } } </style></head><body><p class="content"></p></body></html>
Not all CSS properties can trigger GPU hardware acceleration (layer property changes in the GPU will not trigger repaint), in fact Only a few properties are available, such as the following:
transform
opacity
filter
Canvas animation
<!DOCTYPE html><html><head></head><body><canvas id="canvas" width="700" height="500"></canvas><script> var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var left=0; var timer=setInterval(function () { ctx.clearRect(0,0,700,550); ctx.beginPath(); ctx.fillStyle="#f00"; ctx.fillRect(left,0,100,100); ctx.stroke(); if(left>700){ clearInterval(timer); } left+=1; },16);</script></body></html>
requestAnimationFrame
<!DOCTYPE html><html><head> <style> p{width: 100px;height: 100px;background-color: red;} </style></head><body><p id="box" width="700" height="500"></p><script> window.requestAnimationFrame=window.requestAnimationFrame|| window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; let element=document.getElementById("box"); let left=0; requestAnimationFrame(step); function step() { if(left<window.innerWidth-200){ left+=1; element.style.marginLeft=left+"px"; requestAnimationFrame(step); } }</script></body></html>
The above is the detailed content of JS animation implementation method. 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

AI Hentai Generator
Generate AI Hentai for free.

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



Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

In today's society, mobile phones have become an indispensable part of our lives. As an important tool for our daily communication, work, and life, WeChat is often used. However, it may be necessary to separate two WeChat accounts when handling different transactions, which requires the mobile phone to support logging in to two WeChat accounts at the same time. As a well-known domestic brand, Huawei mobile phones are used by many people. So what is the method to open two WeChat accounts on Huawei mobile phones? Let’s reveal the secret of this method. First of all, you need to use two WeChat accounts at the same time on your Huawei mobile phone. The easiest way is to

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.
