<html>
<head>
<title></title>
<style type="text/css">
#line {
height: 40px;
width: 100%;
background: #ccc;
position: relative;
top: 45%;
}
#box {
width: 100px;
height: 100px;
background: blue;
position: absolute;
top: -30px;
left: 0;
}
</style>
</head>
<body>
<div id="line">
<div id='box'></div>
</div>
<script type="text/javascript">
function animate(opts) {
opts.delay = opts.delay || 15;
opts.duration = opts.duration || 1000;
opts.effect = opts.effect || linear;
var start = new Date;
if ('requestAnimationFrame' in window) {
function draw() {
var now = new Date;
var timePassed = now - start;
var progress = (now - start) / opts.duration;
if (progress < 1) {
var delta = opts.effect(progress);
opts.step(delta);
setTimeout(function() {
requestAnimationFrame(draw);
}, opts.delay);
}
}
draw();
} else {
var id = setInterval(function() {
var timePassed = new Date - start;
var progress = timePassed / opts.duration;
if (progress > 1) progress = 1;
var delta = opts.effect(progress);
opts.step(delta);
if (progress === 1) {
clearInterval(id);
}
}, opts.delay);
}
}
function linear(progress) {
return progress;
}
function quad(progress) {
return Math.pow(progress, 2);
}
function circ(progress) {
return 1 - Math.sin(Math.acos(progress))
}
function back(progress, x) {
x = x || 1.5;
return Math.pow(progress, 2) * ((x + 1) * progress - x)
}
function bounce(progress) {
for (var a = 0, b = 1, result; 1; a += b, b /= 2) {
if (progress >= (7 - 4 * a) / 11) {
return -Math.pow((11 - 6 * a - 11 * progress) / 4, 2) + Math.pow(b, 2)
}
}
}
function elastic(progress, x) {
x = x || 1.5;
return Math.pow(2, 10 * (progress - 1)) * Math.cos(20 * Math.PI * x / 3 * progress)
}
function makeEaseOut(delta) {
return function(progress) {
return 1 - delta(1 - progress)
}
}
function makeEaseInOut(delta) {
return function(progress) {
if (progress < .5)
return delta(2 * progress) / 2
else
return (2 - delta(2 * (1 - progress))) / 2
}
}
var el = document.getElementById('box');
animate({
duration: 3000,
effect: makeEaseInOut(bounce),
step: function(delta) {
el.style.left = 1000 * delta + 'px';
}
});
</script>
</body>
</html>