Created
April 5, 2017 15:29
-
-
Save alireza-saberi/b3117216c2139b00509e454508c2fde4 to your computer and use it in GitHub Desktop.
making a simple animation with js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<style> | |
#container { | |
width: 200px; | |
height: 200px; | |
background: green; | |
position: relative; | |
} | |
#box { | |
width: 50px; | |
height: 50px; | |
background: red; | |
position: absolute; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="container"> | |
<div id="box"> | |
</div> | |
</div> | |
<script> | |
// starting position | |
var pos = 0; | |
//our box element | |
var box = document.getElementById("box"); | |
var t = setInterval(move, 10); | |
function move() { | |
if(pos >= 150) { | |
clearInterval(t); | |
} | |
else { | |
pos += 1; | |
box.style.left = pos+"px"; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment