Eight knowledge points of js motion animation_javascript skills
Today I simply learned js motion animation, recorded my experience and shared it with everyone.
The following are the results I compiled.
Knowledge point 1: Speed animation.
1. The first step is to implement speed motion animation and encapsulate a function. The knowledge used is setInterval(function(){
oDiv.style.left=oDiv.offsetLeft 10 "px";
},30).
As to why offsetLeft is used here, I specifically searched it and the useful information I got is:
The similarity between a.offsetLeft and left is that they represent the left position of the child node relative to the parent node.
b. But left can be read and written, while offsetLeft is read-only;
c. And offsetLeft has no unit, and there is no px behind it when obtaining the position of the child node.
Here is some additional knowledge, thanks to this blogger, http://blog.163.com/hongshaoguoguo@126/blog/static/18046981201372885729561/.
2. Stop the moving node. Here we use the if statement to do a verification. If offsetLeft==0, clearInterval (timer), the timer here should be initialized = null in advance, and then assign the previous motion animation to it.
3. There is a problem here. If the movement is triggered again before the end of the movement, the speed of the movement will be accumulated. Here, as long as clearInterval (timer) is used before the entire movement starts, it will be fine.
4. Set the move-in and removal effect, and set parameters for the movement. One is the speed, and the other is the target position iTarget. We found that the speed can also be judged by the position of ITarget, so only one parameter is needed.
Knowledge point 2: Transparency gradient
1. In fact, it is almost the same as before, except that the value of ITarget is transparency, and the process is still to clear the timer and then open a timer to judge, etc.
2. Define a parameter alpha = transparency. Note that the timer should be written like this:
alpha =speed;
oDiv.style.filter='alpha(opacity:' alpha ')'; //This is a very important method, please note that it is written like this
oDiv.style.opacity=alpha/100; //Be careful not to forget to divide by 100
3. The above are all inline styles.
Knowledge Point 3: Buffering Movement
1. Buffering motion means that the greater the distance, the greater the speed, and the smaller the distance, the smaller the speed, that is, the speed is related to the distance.
2. According to the above statement, redefine the speed. The speed was 0 at the beginning, but now:
var speed=iTarget-oDiv.offsetLeft;
Redefine timer:
oDiv.style.left=oDiv.offsetLeft speed 'px';
At this point we found that the speed was too high, we can do this:
var speed=(iTarget-oDiv.offsetLeft)/10;
3. There will be a serious problem at this time. Because the minimum unit of the screen is px, there will be an iTarget whose final left value is a decimal and not the target. This can be solved through judgment. Math is introduced here. floor(), which rounds down, and Math.ceil(), which rounds up. After defining speed we write like this:
speed=speed>0?Math.ceil(speed):Math.floor(speed);
In this way, it is completely guaranteed that the speeds are all integers and are all 0 at the critical value.
Knowledge point 4: Multi-object motion
1. First get all the objects and form an array, and then use a for loop to do it (how classic this method is!), add node events in the for loop, and use this to replace the current node in the function , eg: startMove(this, iTarget), when defining a function startMove(obj, iTarget).
2. When taking the current width offsetWidth, you must use the value of obj.
3. When the mouse moves very fast, the width of the node cannot be restored to its original state. This is because the timer is a common timer for everyone. The next node has cleared the timer before the previous node has returned to its original state. The solution is to add an index to each node, which is to add aDiv[i].timer=null; in the above for loop; and then replace timer with obj.timer in the defined function. Therefore, we should pay attention to the fact that something will happen to the shared timer.
4. In the movement of transparency, alpha replaces speed, but even if the timer is not shared, problems will arise in the movement of multiple objects. This is because alpha is shared, causing each object to tear each other apart. The solution is to use it like Assign alpha to each node in the for loop like timer.
Summary: To resolve conflicts, either initialize or personalize.
Knowledge point 5. Obtain styles
1. In the timer that changes the width of the node (large for moving in, small for removing), if you add a border to the node, it will be smaller than the target node when moving in, and larger than the target node when moving out. Pay attention to the width padding scrollbar (scroll bar) border, so the reason is that each offset will increase border*2- (the value decreased each time in the timer).
2. The way to solve the above problem is to write width in line and use parseInt(oDiv.style.width) instead of offsetLeft. However, it cannot always be written in line, so we define a function to get the link style:
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr]; //ie browser
}
else{
return getComputerStyle(obj,false)[attr]; //Other browsers
}
}
3. For font-size, there is only one way of writing fontSize in js.
Knowledge point 6: Any attribute value
1. All offset-types will have small bugs. You need to use the getStyle function. This function is often used together with parseInt() and is usually saved in a variable.
2. When writing style.width, you can also write style['width'].
3. For adjusting the attribute values of multiple objects, you can encapsulate the style as a parameter, so that the multi-object attribute function includes the three attribute values (obj, attr, iTarget).
4. The above motion frame is not suitable for transparency changes, because transparency is decimal, for two reasons, the first is parseInt, the second is attr=...px, here we can use a Use if interpretation to process transparency separately, replace parseInt with parseFloat, and remove px.
5. The computer itself has a bug. 0.07*100 is not equal to 7, so we introduce a function called Math.round(), which is a rounded value.
Knowledge point 7: Chain motion
1. Introduce the move.js framework.
2. Pass in a callback function fn(), use if to judge, if there is fn(), then execute fn().
Knowledge point 8: Simultaneous movement
1. If you write two motion functions to control simultaneous motion, function coverage will occur.
2. Use the knowledge point of json. The loop of json uses for (i in json), and the parameters of the motion function are obj, json, fn.
3. There is no iTarget value anymore, it is replaced by json[attr].
As I write this, it’s completely over. I hope you all like it. I also hope it will be helpful for everyone to learn js motion animation.

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


![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

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

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 [

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

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.

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

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
