Animation on GPU vs CPU vs jQuery
by Ali Khaled
HTML
<div id="gpu" class="box">G</div>
<div id="cpu" class="box">C</div>
<div id="jquery" class="box">J</div>
CSS
.box {
-webkit-animation: 3s infinite linear alternate;
background-color: #9cf;
border: solid 1px #99f;
box-shadow:
3px 3px 3px rgba(0,0,0,.2),
0 0 10px rgba(0,0,0,.2) inset;
height: 50px;
line-height: 50px;
margin: 4px;
position: relative;
text-align: center;
width: 50px;
}
#gpu {
-webkit-animation-name: move-by-transform;
}
@-webkit-keyframes move-by-transform {
0% { -webkit-transform:translateX(0) }
100% { -webkit-transform:translateX(200px) }
}
#cpu {
-webkit-animation-name: move-by-left;
}
@-webkit-keyframes move-by-left {
0% { left:0 }
100% { left:200px; }
}
JavaScript
var $box = $('#jquery');
function goToRight() {
$box.animate({ left:200 }, 3000, 'linear', goToLeft);
}
function goToLeft() {
$box.animate({ left: 0 }, 3000, 'linear', goToRight);
}
goToRight();