Home >Web Front-end >JS Tutorial >Example of automatic movement of static elements using JS_javascript skills

Example of automatic movement of static elements using JS_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:52:221075browse

An element is stationary, causing it to move automatically on the screen.

This is a relatively simple question. I encountered it while studying, so I wrote it down.

Copy code The code is as follows:





<script> <br>var timenum; <br>var mar=0;//Control movement Variable of amount<br>var flag = 0;//A variable to control left and right movement<br>//Function to move to the right<br>function moveright(){ <br>sp1.style.marginLeft=mar " px"; <br>mar=mar 5; <br>} <br>//Function to move to the right<br>function moveleft(){ <br>sp1.style.marginLeft=mar "px"; <br>mar=mar-5; <br>} <br><br>function go() { <br>var sp1 =document.getElementById("sp1"); <br>var btn1 = document.getElementById("start" ); <br>if(!btn1.disabled){ <br>btn1.disabled = true; <br>document.getElementById("pause").disabled=false; <br>} <br>sp1.innerHTML=parseInt (sp1.innerHTML) 1; <br>timenum = window.setTimeout(go,10); <br>if(flag==1){ <br>window.setTimeout(moveleft,10); <br>} <br>if(flag==0){ <br>window.setTimeout(moveright,10); <br>} <br>if(mar>(window.outerWidth)){ <br>flag=1; <br>} <br>if(mar<0){ <BR>flag=0; <BR>} <BR>} <BR>function stop(){ <BR>document.getElementById("start").disabled = false; <BR>document.getElementById("pause").disabled=true; <BR>window.clearTimeout(timenum); <BR>} <BR></script>

< body>






Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn