使用 CSS3 从最后一个位置随机旋转图像
我想要一个“命运之轮”的效果。当用户单击图像时,它会从其最后位置旋转随机角度(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 编写答案受到高度赞赏。
I'd like to have a "Wheel of Fortune" effect. When user clicks the image, it rotates random degrees (between 180-540 degrees) from its last position. Rotation should be done with CSS3. Here's my code so far:
<!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>
Problems are:
How to randomly change the value of rotation in keyframes?
How to continue rotation from its last position?
At the moment animRes() is reseting animation so the -webkit-animation-fill-mode: forwards
doesn't work but without animRes() I can't reset the animation for more spins. Writing the answer with jQuery and plain JS is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要学习一些数学知识才能实现你想要做的事情,希望它不会太难理解......
我使用 CSS 过渡而不是动画,因为它们更简单。
如果要转换的度数更多,则毫秒 = ... 公式会使转换持续时间更长。希望您可以将所有这些集成到您已经编写的代码中。
您可以在此处查看演示:http://jsfiddle.net/XkNrf/
You need to get into some math to achieve what you want to do, hopefully it's not too difficult to understand...
I'm using CSS Transitions instead of animations because they are more simple.
The miliseconds = ... formula makes the transition last longer if there's more degrees to be transitioned. Hopefully you can integrate all of this into the code you already wrote.
You can see a demo here: http://jsfiddle.net/XkNrf/
我不会这样处理。
我做了一个小例子 here
它使用 -moz 属性,但它们都有一个等效的 webkit,所以它应该没问题。
想法如下:您的图像以无限的方式旋转,但大部分时间都暂停(使用
-moz-animation-play-state
。当用户点击它时,它开始运行。在a和b之间生成一个随机数。然后它旋转X*Y+Z 毫秒(在示例中,a 和 b 的值为 1 和 11,Y 为 1000,Z 为 0),然后再次暂停,这样您可以轻松自定义可能结果的数量和最短旋转时间 X。
。您还可以使用从 0 开始的外部变量来存储当前值,每次用户单击时该变量都会增加
I wouldn't process this way.
I made a little example here
It uses -moz properties, but they all have a webkit equivalent, so it should be fine.
The idea is the following: your image is rotating in an infinite fashion BUT is paused most of the time (using
-moz-animation-play-state
. When the user click it, it starts running. A random number is generated between a and b. And then it rotates for X*Y+Z milliseconds (in the example a and b have a 1 and 11 value, and Y is 1000, Z is 0) then it pauses again.This way you can customize easily the number of possible outcomes and the minimum rotation time.
And you can also store the current value with an external variable starting at 0 and that's incremented by X each time an user click.