Skip to content

Instantly share code, notes, and snippets.

@alireza-saberi
Created April 5, 2017 15:29
Show Gist options
  • Save alireza-saberi/b3117216c2139b00509e454508c2fde4 to your computer and use it in GitHub Desktop.
Save alireza-saberi/b3117216c2139b00509e454508c2fde4 to your computer and use it in GitHub Desktop.
making a simple animation with js
<!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