_javascript techniques for implementing multi-object buffering motion instance code in JS

Source: Internet
Author: User
Tags setinterval

Effect:

Ideas:

Using the setinterval timer for movement, offsetwidth to realize the width of the change, in the onmouseover with the end point and the selected div into the parameters of the buffer movement.

Code:

Copy Code code as follows:

<title></title>
<style type= "Text/css" >
Div
{
width:100px;
height:50px;
Background: #0000FF;
margin:10px;
}
</style>
<script type= "Text/javascript" >
Window.onload = function () {
var odiv = document.getelementsbytagname (' div ');
for (var i = 0; i < odiv.length; i++) {
Odiv[i].timer = null; A tag to buy a div to turn off the timer for the corresponding Div
Odiv[i].onmouseover = function () {
Move (this, 400); Output parameters to the timer
}
Odiv[i].onmouseout = function () {
Move (this, 100);
}
}
};
function Move (Div, end) {
Clearinterval (Div.timer);
Div.timer = setinterval (function () {
var speed = (end-div.offsetwidth)/5; (End point-the width to go)/scaling factor =div moving speed
Speed = speed > 0?   Math.ceil (Speed): Math.floor (speed); Decimal rounding, that is, carrying rounding.
if (Div.offsetwidth = end) {//Shutdown Timer when destination is reached
Clearinterval (Div.timer);
}
else {
Div.style.width = div.offsetwidth + speed + ' px '; Move the width of the div
}
}, 30)
}
</script>
<body>
<div>
</div>
<div>
</div>
<div>
</div>
</body>

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.