Detailed introduction to implementing JavaScript animations
Preface
Nowadays, many pages have some animation effects. Appropriate animation effects can improve the beauty of the page to a certain extent, and animations with prompt effects can enhance the usability of the page.
There are two ways to implement page animation. One is to indirectly operate the CSS style by operating JavaScript and update every once in a while; the other is to directly define the animation through CSS. The second method was widely adopted after CSS3 matured. In this article, we discuss the principles and implementation of the first approach.
JavaScript animation implementation principleFirst of all, we need to know two important concepts, animation time process and animation effect process. The animation time process refers to the completion of the animation in terms of time, which is a number between [0, 1]. Assume that the animation starts at timestamp t1 and ends at
t2, and the current timestamp is
t, then the current time progress of the animation is
(t-t1)/(t2-t1). If you can't understand it, I suggest you draw it with pen and paper. Understanding this concept is crucial to understanding this article.
attribute value being animated. Suppose we want to change the CSS left attribute of the
#el<a href="http://www.php.cn/wiki/907.html" target="_blank"> element from </a>100px
to 200px
. It has already changed When it reaches 130px
, the current effect process of the animation is 130px - 100px = 30px
. Assume that the animation time process and animation effect process are both linear. So if you know the animation time process, you can definitely get the animation effect process.
According to this explanation, we can quickly write a linear animation.
(function() { var begin, // 开始动画的时间 el, start, end, duration; var INTERVAL = 13; function now() { return (new Date).getTime(); } /** * 执行一步动画(更新属性) */ function _animLeft() { var pos = (now() - begin) / duration; if (pos >= 1.0) { return false; } return !!(el.style.left = start + (end - start) * pos); } /** * 对一个DOM执行动画,left从_start到_end,执行时间为 * _duration毫秒。 * * @param {object} _el 要执行动画的DOM节点 * @param {integer} _start left的起始值 * @param {integer} _end left的最终值 * @param {integer} _duration 动画执行时间 */ function animLeft(_el, _start, _end, _duration) { stopped = false; begin = now(); el = _el; start = _start; end = _end; duration = _duration || 1000; var step = function() { if (_animLeft()) { setTimeout(step, INTERVAL); } }; setTimeout(step, 0); } window.animLeft = animLeft; })(); animLeft( document.getElementById('el'), 100, 200 )
JSBin
easing
Many times, the animation we need is not linear. The so-called nonlinearity, intuitively speaking, means that the animation speed changes with time. So how to implement variable speed animation?
As mentioned above, we know that controlling the time process of animation is equivalent to controlling the effect process of animation. As the time progresses in the real world, the time progress of the animation will follow, thereby controlling the effect progress of the animation. Then, we can control the animation process by modifying the mapping relationship between the real world time process and the animation time process. If you are confused, it doesn’t matter, please look at the picture below:
This is the mapping relationship between the real-world time process and the animation process in linear animation. Next, we transform it
This curve is actually the image of the
y = x * x. It can be seen that the definition domain and value range of the two curves have not changed. The slope of the curve is the speed of the animation. Next, we overlap the two pictures for comparison.
When the real world time reaches
, the animation process should originally proceed to y0
, after the transformation , only proceeds to y1
. In the end, all rivers return to the sea, and the two lines meet at point (1, 1). Here, y = x * x
is the easing function (easing function)
. Let’s modify the above example to make the animation non-linear.
function ease(time) { return time * time; } /** * 执行一步动画(更新属性) */ function _animLeft() { var pos = (now() - begin) / duration; if (pos >= 1.0) { return false; } pos = ease(pos); return !!(el.style.left = (start + (end - start) * pos) + "px"); }
JSBin
We can see such a function in the code of
jQuery. jQuery.easing = {
linear: function( p ) {
return p;
},
swing: function( p ) {
return 0.5 - Math.cos( p * Math.PI ) / 2;
}
};
jQuery.easing.myEasing = function( p ) { return ... }
Summary
JavaScript animation essentially executes animation by operating CSS. The time progression of the animation can determine the effect progression of the animation. By manipulating the relationship between the time course of the real world and the time course of the animation, we can transform linear animation into non-linear animation.
The above is the detailed content of Detailed introduction to implementing JavaScript animations. 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


![Animation not working in PowerPoint [Fixed]](https://img.php.cn/upload/article/000/887/227/170831232982910.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
Are you trying to create a presentation but can't add animation? If animations are not working in PowerPoint on your Windows PC, then this article will help you. This is a common problem that many people complain about. For example, animations may stop working during presentations in Microsoft Teams or during screen recordings. In this guide, we will explore various troubleshooting techniques to help you fix animations not working in PowerPoint on Windows. Why aren't my PowerPoint animations working? We have noticed that some possible reasons that may cause the animation in PowerPoint not working issue on Windows are as follows: Due to personal

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

We often use ppt in our daily work, so are you familiar with every operating function in ppt? For example: How to set animation effects in ppt, how to set switching effects, and what is the effect duration of each animation? Can each slide play automatically, enter and then exit the ppt animation, etc. In this issue, I will first share with you the specific steps of entering and then exiting the ppt animation. It is below. Friends, come and take a look. Look! 1. First, we open ppt on the computer, click outside the text box to select the text box (as shown in the red circle in the figure below). 2. Then, click [Animation] in the menu bar and select the [Erase] effect (as shown in the red circle in the figure). 3. Next, click [

This website reported on January 26 that the domestic 3D animated film "Er Lang Shen: The Deep Sea Dragon" released a set of latest stills and officially announced that it will be released on July 13. It is understood that "Er Lang Shen: The Deep Sea Dragon" is produced by Mihuxing (Beijing) Animation Co., Ltd., Horgos Zhonghe Qiancheng Film Co., Ltd., Zhejiang Hengdian Film Co., Ltd., Zhejiang Gongying Film Co., Ltd., Chengdu The animated film produced by Tianhuo Technology Co., Ltd. and Huawen Image (Beijing) Film Co., Ltd. and directed by Wang Jun was originally scheduled to be released in mainland China on July 22, 2022. Synopsis of the plot of this site: After the Battle of the Conferred Gods, Jiang Ziya took the "Conferred Gods List" to divide the gods, and then the Conferred Gods List was sealed by the Heavenly Court under the deep sea of Kyushu Secret Realm. In fact, in addition to conferring divine positions, there are also many powerful evil spirits sealed in the Conferred Gods List.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

According to news from this site, Miyazaki Hayao's animated film "Porco Rosso" has announced that it will extend the release date to January 16, 2024. This site previously reported that "Porco Rosso" has been launched in the National Art Federation Special Line Cinema on November 17, with a cumulative box office of over 2,000 10,000, with a Douban score of 8.6, and 85.8% of 4 and 5 star reviews. "Porco Rosso" was produced by Studio Ghibli and directed by Hayao Miyazaki. Moriyama Moriyama, Tokiko Kato, Otsuka Akio, Okamura Akemi and others participated in the dubbing. It was originally released in Japan in 1992. The film is adapted from Hayao Miyazaki's comic book "The Age of Airships" and tells the story of the Italian Air Force's ace pilot Pollock Rosen who was magically turned into a pig. After that, he became a bounty hunter, fighting against air robbers and protecting those around him. Plot synopsis: Rosen is a soldier in World War I

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.
