我正在制作轮盘游戏,但我遇到了一个问题,我的动画"(或你所说的)只能玩一次。我没有这么多javascript的经验,也不知道如何解决我的问题。
var img = document.querySelector('#ball');
ball.addEventListener('click', onClick, false);
function onClick() {
this.removeAttribute('style');
var deg = 1080;
var css = '-webkit-transform: rotate(' + deg + 'deg);';
this.setAttribute(
'style', css
);
}

.roulette{
padding-top: 5em;
padding-left: 5em;
}
.roulette img{
position: absolute;
height: 50em;
}
.wheel{
}
.ball{
z-index: 1;
}
.border{
z-index: 1;
}
#ball {
-webkit-transition: -webkit-transform 4s ease-out;
z-index: 1;
}

<h1>Press the ball</h1>
<div class="roulette">
<img class="border" src="http://www.mediafire.com/convkey/0a20/z78myz6x2nk17n76g.jpg">
<img id="ball" src="http://www.mediafire.com/convkey/8eec/69mn483g0moovs66g.jpg">
<img class="wheel" id="spin2" src="http://www.mediafire.com/convkey/937f/f4f65gia76l409u6g.jpg">
</div>
&#13;
答案 0 :(得分:3)
你必须在每次跑步时改变度数。比如...
var img = document.querySelector('#ball');
ball.addEventListener('click', onClick, false);
var deg = 0;
function onClick() {
this.removeAttribute('style');
deg += 1080;
var css = '-webkit-transform: rotate(' + deg + 'deg);';
this.setAttribute(
'style', css
);
}
.roulette{
padding-top: 5em;
padding-left: 5em;
}
.roulette img{
position: absolute;
height: 50em;
}
.wheel{
}
.ball{
z-index: 1;
}
.border{
z-index: 1;
}
#ball {
-webkit-transition: -webkit-transform 4s ease-out;
z-index: 1;
}
<h1>Press the ball</h1>
<div class="roulette">
<img class="border" src="http://www.mediafire.com/convkey/0a20/z78myz6x2nk17n76g.jpg">
<img id="ball" src="http://www.mediafire.com/convkey/8eec/69mn483g0moovs66g.jpg">
<img class="wheel" id="spin2" src="http://www.mediafire.com/convkey/937f/f4f65gia76l409u6g.jpg">
</div>
或设置超时以确保在添加属性之前删除该属性:
var img = document.querySelector('#ball');
ball.addEventListener('click', onClick, false);
function onClick() {
that = this;
this.removeAttribute('style');
setTimeout(function() {
var css = '-webkit-transform: rotate(1080deg);';
that.setAttribute(
'style', css
);
}, 0);
}
.roulette {
padding-top: 5em;
padding-left: 5em;
}
.roulette img {
position: absolute;
height: 50em;
}
.wheel {}
.ball {
z-index: 1;
}
.border {
z-index: 1;
}
#ball {
-webkit-transition: -webkit-transform 4s ease-out;
z-index: 1;
}
<h1>Press the ball</h1>
<div class="roulette">
<img class="border" src="http://www.mediafire.com/convkey/0a20/z78myz6x2nk17n76g.jpg">
<img id="ball" src="http://www.mediafire.com/convkey/8eec/69mn483g0moovs66g.jpg">
<img class="wheel" id="spin2" src="http://www.mediafire.com/convkey/937f/f4f65gia76l409u6g.jpg">
</div>