Javascript-javascript tips for automatically moving static elements

Source: Internet
Author: User
This article mainly introduces how to implement automatic moving of static elements in Javascript. For more information, see if the next element is static, so that it can be automatically moved on the screen.

This is a simple problem. I encountered it during my learning and wrote it down.

The Code is as follows:








Script
Var timenum;
Var mar = 0; // variable used to control the movement volume
Var flag = 0; // a variable that controls left and right movement
// Implement the function of moving to the right
Function moveright (){
Sp1.style. marginLeft = mar + "px ";
Mar = mar + 5;
}
// Implement the function of moving to the right
Function moveleft (){
Sp1.style. marginLeft = mar + "px ";
Mar = mar-5;
}

Function go (){
Var sp1 = document. getElementById ("sp1 ");
Var btn1 = document. getElementById ("start ");
If (! Btn1.disabled ){
Btn1.disabled = true;
Document. getElementById ("pause"). disabled = false;
}
Sp1.innerHTML = parseInt (sp1.innerHTML) + 1;
Timenum = window. setTimeout (go, 10 );
If (flag = 1 ){
Window. setTimeout (moveleft, 10 );
}
If (flag = 0 ){
Window. setTimeout (moveright, 10 );
}
If (mar> (window. outerWidth )){
Flag = 1;
}
If (mar <0 ){
Flag = 0;
}
}
Function stop (){
Document. getElementById ("start"). disabled = false;
Document. getElementById ("pause"). disabled = true;
Window. clearTimeout (timenum );
}
Script


Pause


0

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.