1.使用tween的案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="target" style="position:absolute; top: 100px; left: 100px; width: 100px; height: 100px; background: #a0dde9; padding: 1em;">
hello world!
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/16.3.5/Tween.min.js"></script>
<script>
var position;
var target;
var tween,tweenBack;
init();
animate();
function init(){
position={x:100,y:100,rotation:0};
target=document.getElementById('target');
//动画从0,0位置到700,200的位置旋转360
tween=new TWEEN.Tween(position)
.to({x:700,y:200,rotation:360},2000)
.onUpdate(update);
tween.start();
}
function animate( time ) {
var id = requestAnimationFrame(animate);
var result = TWEEN.update(time);
if(!result) cancelAnimationFrame(id);
}
function update() {
target.style.left = position.x + 'px';
target.style.top = position.y + 'px';
target.style.webkitTransform = 'rotate(' + Math.floor(position.rotation) + 'deg)';
target.style.MozTransform = 'rotate(' + Math.floor(position.rotation) + 'deg)';
}
</script>
</body>
</html>