【发布时间】:2011-08-31 03:28:06
【问题描述】:
我想要一个“幸运之轮”效果。当用户单击图像时,它会从最后一个位置随机旋转角度(180-540 度之间)。旋转应使用 CSS3 完成。到目前为止,这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#pic {
position: absolute;
top: 200px;
left: 200px;
}
@-webkit-keyframes spin
{
100% {-webkit-transform: rotateZ(300deg);}
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function animRes() {
var $element = $("#pic").bind("webkitAnimationEnd", function(){
this.style.webkitAnimationName = "";
});
}
function doSpin() {
animRes();
$("#pic").css('-webkit-animation', 'spin 1s ease-out');
$("#pic").css('-webkit-animation-fill-mode', 'forwards'); //doesn't work when using AnimRes()
}
</script>
</head>
<body>
<img id="pic" src="picture.jpg" alt="pic" onclick="doSpin();"/>
</body>
</html>
问题是:
如何随机改变关键帧中的旋转值?
如何从上一个位置继续旋转?
目前 animRes() 正在重置动画,因此 -webkit-animation-fill-mode: forwards 不起作用,但没有 animRes() 我无法重置动画以获得更多旋转。非常感谢使用 jQuery 和纯 JS 编写答案。
【问题讨论】:
标签: javascript animation random css rotation