DOM-Simple Animation

<!DOCTYPE html>
<html>
<head>
    <style>
   #container{background-color:teal;
    border-radius:1cm;
    width:800px;
    height:400px;
    position:relative;
}
#box{
    background-color:orange;
    border-radius:4cm;
    width:400px;
    height:200px;
    position:absolute;
}
#container1{background-color:skyblue;       
    border-radius:1cm;
    width:800px;
    height:400px;
    position:relative;
}
#box1{
    background-color:green;
    border-radius:4cm;
    width:400px;
    height:200px;
    position:absolute;
}
    </style>
    
    <title></title>
</head>
<body>
<div id="container" style="text-align:center;">This will be stationary (: 
<div id="box" style="text-align:center;">This will swing!!!!!!!</div> 
</div>
<br><br>
<div id="container1" style="text-align:center;">This will be stationary (:
<div id="box1" style="text-align:center;">This will move to left and come to rest.</div>    
</div>



</body>
<script type="text/javascript">
var pos=1;
var box=document.getElementById("box");
var pos1=400;
function swing(){
    if(pos<=400){
        pos++;
        box.style.left=pos+"px";
    }
    else if(pos<=800){
        pos++;
        pos1--;
        box.style.left=pos1+"px";
    }
    else{
        box.style.left="1px";
        pos=1;
        pos1=400;
    }
}
setInterval(swing,5);

var posi=1;
var box1=document.getElementById("box1");
function move(){
    if (posi<=400){
        posi++;
        box1.style.left=posi+"px";
    }
    else{
        box1.style.left="1px";
        closeInterval(t);
    }
}
var t=setInterval(move,10);



</script>
</html>

Comments