On the stopping condition of uniform motion in JavaScript--basic knowledge

Source: Internet
Author: User
Tags abs setinterval

We first look at the previous uniform Motion Code, modified the speed speed will appear after what kind of a bug. Two benchmarks are added to the test.

Copy Code code as follows:

<style type= "Text/css" >
#div1 {
width:100px;
height:100px;
Position:absolute;
background:red;
top:50px;
left:600px;
}
#div2 {
width:1px;
height:300px;
Position:absolute;
left:300px;
top:0;
Background:black;
}
#div3 {
width:1px;
height:300px;
Position:absolute;
left:100px;
top:0;
Background:black;
}
</style>
<script type= "Text/javascript" >
var time = null;
function Startmove (itarget) {
var odiv = document.getElementById ("Div1");
Clearinterval (time);
Time = SetInterval (function () {
var speed = 0;
if (Odiv.offsetleft < ITarget) {
Speed = 7;
} else {
Speed =-7;
}
Actually, this is a problem.
ODiv.style.left = odiv.offsetleft + speed + ' px ';

}, 30)
}
</script>
<body>
<input type= "button" id= "BTN" value= "to M" onclick= "Startmove" (MB) "/>"
<input type= "button" id= "btn" "value=" to "onclick=" (=) startmove
<div id= "Div1" >
</div>
<div id= "Div2" >
</div>
<div id= "Div3" >
</div>
</body>

In fact, such a code if the speed to change to 7 of this odd number, but to reach the target is an integer, which can not reach the target point or exceed the target point back and forth jitter of the bug

So why is this happening?

In fact, when he reached the target point can not be accurate to the target point, if the target point is 100, each walk 7, this time he is either over the target point, or is not.

Never reach the target point. In fact, the buffer before the help is a bit like.

So what exactly is the point of reaching the target?

For example: You take a taxi to a place, the driver must be to where almost 10.2-meter meters away from a stop, even if the. It's impossible to ask for a car to stick to that place.

So, in fact, the program is the same, we as long as the distance between the object and the target point near to a certain degree, it does not need to close, that is.

Let's look at the modified code:

Copy Code code as follows:

<script type= "Text/javascript" >
var time = null;

function Startmove (itarget) {
var odiv = document.getElementById ("Div1");
Clearinterval (time);
Time = SetInterval (function () {
var speed = 0;
if (Odiv.offsetleft < ITarget) {
Speed = 7;
} else {
Speed =-7;
}
if (Math.Abs (itarget-odiv.offsetleft) <= 7) {
Clearinterval (time);
odiv.style.left=itarget+ ' px ';
} else {
ODiv.style.left = odiv.offsetleft + speed + ' px ';
}

}, 30)
}
</script>

Explain: Why use Math.Abs to take absolute value here?

The reason is simple, because the speed may be positive and may be negative.

Now we have the distance between the target and the object as long as it is less than 7. Why is it 7? Because his next exercise is less than 7. This time we'll even have him at the target point.

So now the question is here, so he doesn't exactly stop at the target point. So we add a simple word, just let left equal to the target point. odiv.style.left=itarget+ ' px ';

In fact, the last walk of less than 7, but we all know that the program is running too fast, people can not see the eyes. A warm smile.

There is no problem at this time. Blink

This is the stopping condition of uniform motion. Then a friend asked, why is the buffer movement not so troublesome?

Because his speed is changed, getting smaller, until finally he even reached 1, step by step forward certainly will not appear such a problem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.