search
HomeWeb Front-endJS TutorialEight 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(){

Copy code The code is as follows:

  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:

Copy code The code is as follows:

​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:

Copy code The code is as follows:

var speed=iTarget-oDiv.offsetLeft;

Redefine timer:

Copy code The code is as follows:

oDiv.style.left=oDiv.offsetLeft speed 'px';

At this point we found that the speed was too high, we can do this:

Copy code The code is as follows:

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:

Copy code The code is as follows:

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:

Copy code The code is as follows:

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.

Statement
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
Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

See all articles

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function