html - How to loop this javascript object? -


i have code, simple code, want automatically right , loop again (from right left, @ right side of window showed in left side again) right code make object infinitely move left right possible?

var imgobj = null;  var animate;    function init() {    imgobj = document.getelementbyid('myimage');    imgobj.style.position = 'relative';    imgobj.style.left = '0px';  }    function moveright() {    imgobj.style.left = parseint(imgobj.style.left) + 10 + 'px';    animate = settimeout(moveright, 20); // call moveright in 20msec  }    function stop() {    cleartimeout(animate);    imgobj.style.left = '0px';  }    window.onload = init;
<html>  <head>    <title>javascript animation</title>  </head>    <body>    <form>      <img id="myimage" src="/images/html.gif" />      <p>click buttons below handle animation</p>      <input type="button" value="start" onclick="moveright();" />      <input type="button" value="stop" onclick="stop();" />    </form>  </body>  </html>

when image past width of window, needs have position reset zero. can modulus operator.

var imgobj = null;  var animate;    function init() {    imgobj = document.getelementbyid('myimage');    imgobj.style.position = 'relative';    imgobj.style.left = '0px';  }    function moveright() {    var bodywidth = document.body.clientwidth;    var newleft = parseint(imgobj.style.left) + 10    imgobj.style.left = newleft % bodywidth + 'px';    animate = settimeout(moveright, 20); // call moveright in 20msec  }    function stop() {    cleartimeout(animate);    imgobj.style.left = '0px';  }    window.onload = init;
<html>    <head>    <title>javascript animation</title>        </head>    <body>      <form>      <img id="myimage" src="/images/html.gif" />      <p>click buttons below handle animation</p>      <input type="button" value="start" onclick="moveright();" />      <input type="button" value="stop" onclick="stop();" />    </form>    </body>    </html>


Comments