Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
<html>
<head>
<script>
(function() {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
var d = new Array(2), t0;
function start(){
t0 = window.mozAnimationStartTime;
if(!t0) t0 = Date.now();
d[0] = document.getElementById("c1");
d[1] = document.getElementById("c2");
requestAnimationFrame(step);
}
function step(ts){
var dt = ts - t0;
var X = 100*Math.sin(dt/1000), Y = 150*Math.cos(dt/1000);
d[0].style.transform = "translate("+X+"px,"+Y+"px)";
d[1].style.transform = "translate("+(-X)+"px,"+Y+"px)";
/*d[0].style.top = 300 + X + "px"; d[0].style.left = 300 + Y + "px";
d[1].style.top = 300 - X + "px"; d[1].style.left = 300 + Y + "px";*/
requestAnimationFrame(step);
}
</script>
<style>
.container{
position: absolute;
width: 100px;
height: 100px;
top: 300px;
left: 300px;
}
.ball{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
background: #ff9999;
box-shadow: inset 10px -15px 50px 15px #990000;
z-index: 10;
}
.ballshadow{
position: absolute;
bottom: 0;
right: 20%;
width: 100%;
height: 30%;
background: black;
border-radius: 60%;
box-shadow: 0 0 10px 2px;
z-index: 0;
}
</style>
</head>
<body onload="start();">
<div class="container" id="c1"><div class="ballshadow"></div><div class="ball"></div></div>
<div class="container" id="c2"><div class="ballshadow"></div><div class="ball"></div></div>
</body>
</html>
Почему перемещать элементы с помощью translate лучше, чем с position:absolute top/left