Notes on JavaScript buffered motion

Source: Internet
Author: User

JS inside about the movement of the framework is more, starting from the basic, I feel that the buffer movement is more important than the teaching basis, first provide a small example.

<script type= "Text/javascript" >
Window.onload = function ()
{
var obtn = document.getElementById ("btn1");
var odiv = document.getElementById ("Div1"); Get div by ID
Obtn.onclick = function ()
{
Startmove (310); Set the target value, both the location to reach
}
function Startmove (itarget)//Incoming target value, movement to where
{
Clearinterval (timer); Before opening a timer, first initialize, clear the open, to avoid the impact
var timer = null;
Timer = setinterval (function () {
var ispeed = (itarget-odiv.offsetleft)/10; Before you use offsetleft, you need to position the operand with a LETF value
Ispeed = ispeed>0? Math.ceil (ispeed): Math.floor (Ispeed);
General If,else statements can be written as three-mesh operators
if (ispeed>0) {
Ispeed = Math.ceil (ispeed); Rounding up so that the movement speed is rounded so that the operand stops before it reaches the target.
}else{
Ispeed = Math.floor (ispeed); Rounding down
// }

if (odiv.offsetleft==itarget)
{
Clearinterval (timer);
}else
{
ODiv.style.left = odiv.offsetleft + ispeed + ' px ';
}
Document.title = ODiv.style.left;
},30);

};
};

</script>

This is about the buffer movement more practical ideas, should also be in the future, the most important thing is to understand the relationship between the target value and speed

Notes on JavaScript buffered motion

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.