Home > Web Front-end > JS Tutorial > body text

Detailed explanation of JS uniform motion examples

小云云
Release: 2018-03-16 16:11:58
Original
1378 people have browsed it

This article mainly shares with you detailed examples of JS uniform motion. I hope it can help you. Let us first introduce the basic principles of JS motion to you.

Basic principles of JS movement:

To make p move, the key is to modify the coordinates of the object,

op.style.left=offsetLeft+speed+'px';
Copy after login

But This can only be moved once. We can use the timer to make this operation 'move'.

setInterval(funtion(){
op.style.left=offsetLeft+speed+'px'; (speed是每次移动的像素)
},30)
Copy after login

This way you can exercise, but the effect is not what we need. The source code is as follows:

Detailed explanation of JS uniform motion examples

To stop a moving object: the key is to determine the size of offsetLeft and turn off the timer;

var timer=null;
time=setInterval(function(){
                     if(op.offsetLeft>=300){
clearInterval(timer);
                         }else{
op.style.left=op.offsetLeft+10+‘px’;}
},30)这样还是有小瑕疵,就是多次点击造成的BUG,解决方法就是关闭上次的定时器:多加个clearInterval(timer)
Copy after login

Related Recommendation:

How to create a universal uniform motion framework

Explanation of uniform motion based on js

JavaScript Detailed introduction to uniform speed motion and variable speed (buffer) motion in _Basic knowledge

The above is the detailed content of Detailed explanation of JS uniform motion examples. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template