Home > Web Front-end > JS Tutorial > Javascript simulates acceleration and deceleration motion code sharing_javascript skills

Javascript simulates acceleration and deceleration motion code sharing_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:27:33
Original
1495 people have browsed it

Accelerating motion means that an object moves faster and faster; decelerating motion means that an object moves slower and slower. Now use Javascript to simulate these two effects. The principle is to use setInterval or setTimeout to dynamically change the distance between one element and another element, such as xxx.style.left or xxx.style.marginLeft, and then increase the speed after each movement. In this way, the effect of accelerating motion appears, and the same is true for decelerating motion.

Here are two examples:

Accelerate movement

Copy code The code is as follows:





Javascript accelerates motion








Note: In this example, after clicking GO, the div block will accelerate to the right

Deceleration movement

Copy code The code is as follows:





Javascript deceleration motion








Note: In this example, after clicking GO, the div block will keep decelerating to the right until the speed reduces to zero, the speed becomes a negative value, and then accelerates to the left

Related labels:
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template