The realization _javascript skill of Javascript multi-object motion

Source: Internet
Author: User
Tags setinterval

Let's take a look at the previous code of motion and whether it supports the motion of multiple objects.

Copy Code code as follows:

<style type= "Text/css" >
div {
width:100px;
height:50px;
background:red;
margin:10px;
}
</style>

Copy Code code as follows:

<body>
<div></div>
<div></div>
<div></div>
</body>

Here's the JavaScript code:

Copy Code code as follows:

<script type= "Text/javascript" >
Window.onload = function () {
var adiv = document.getelementsbytagname (' div ');
for (var i = 0; i < adiv.length; i++) {
Adiv[i].onmouseover = function () {
Startmove (this, 400);
};
Adiv[i].onmouseout = function () {
Startmove (this, 100);
};
}
}
var timer = null;
function Startmove (obj, itarget) {
Clearinterval (timer);
Timer = setinterval (function () {
var speed = (itarget-obj.offsetwidth)/6;
Speed = speed > 0? Math.ceil (Speed): Math.floor (speed);
if (obj.offsetwidth = = ITarget) {
Clearinterval (timer);
} else {
Obj.style.width = obj.offsetwidth + speed + ' px ';
}
}, 30);
}
</script>

When the mouse moves to the first Div, he is running normally. But if you move to a second or third Div now, a bug will appear.

Image what is the reason for this? Looking at the picture shows that there is no movement to complete. Actually, it is.

The whole program on a timer, such as the first Div started moving, the second Div mouse moved into the previous timer was killed, then nature is stuck there.

So the biggest problem is that the entire program has only one timer. So how do we solve this problem?

Solution:

In fact, it is very simple, the timer as a property of an object plus, then each object has a timer, when the timer is closed when the timer on the object, open is also the timer on the object

Then they can run completely undisturbed.

Look at the modified JavaScript code:

Copy Code code as follows:

<script type= "Text/javascript" >
Window.onload = function () {
var adiv = document.getelementsbytagname (' div ');
for (var i = 0; i < adiv.length; i++) {
Adiv[i].timer=null; To save a timer as a property of an object
Adiv[i].onmouseover = function () {
Startmove (this, 400);
};
Adiv[i].onmouseout = function () {
Startmove (this, 100);
};
}
}
function Startmove (obj, itarget) {
Clearinterval (Obj.timer);
Obj.timer = setinterval (function () {
var speed = (itarget-obj.offsetwidth)/6;
Speed = speed > 0? Math.ceil (Speed): Math.floor (speed);
if (obj.offsetwidth = = ITarget) {
Clearinterval (Obj.timer);
} else {
Obj.style.width = obj.offsetwidth + speed + ' px ';
}
}, 30);
}
</script>

So the program is no problem, can support the movement of multiple objects.

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.