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