Javascript setInterval() Move element

Description

Javascript setInterval() Move element

View in separate window

<!DOCTYPE html>
<head>
<style>
#redbox//from  w  w w .j av  a2s . c om
{
  width: 100px; height: 100px;
  background-color: red;
  position: absolute;
  left: 0; top: 100px;
}
</style>
<script>

window.onload=function() {
    let x = 0;
    setInterval(moveElement,1000);
    
    function moveElement() {
      x+=10;
      let left = x + "px";
      document.getElementById("redbox").style.left=left;
    }
}
</script>

</head>
<body>
<div id="redbox"></div>
</body>
</html>



PreviousNext

Related