Move Element with set Interval of Time

Move Element with set Interval of Time

by rajnishw3

HTML

<div id="animatedElem"><div>

CSS

#animatedElem {
    position: absolute;
    width: 20px;
    height: 20px;
    background: #ff6600;
}

JavaScript

var elem = document.getElementById("animatedElem"),
  left = 0,
  timer;
// Move the element 10px on the right every 1s
timer = setInterval(function() {
  elem.style.left = ( left += 10 ) + "px";
  // clear the timer at 400px to stop the animation
  if ( left == 500 ) {
    clearInterval( timer );
  }
}, 1000);