admin管理员组文章数量:1425221
So I made this,
var Coin = document.getElementById("coin");
Coin.onclick = function() {
Coin.style.webkitTransform = "rotateY(1800deg)";
Coin.style.MozTransform = "rotateY(1800deg)";
Coin.style.msTransform = "rotateY(1800deg)";
Coin.style.OTransform = "rotateY(1800deg)";
Coin.style.transform = "rotateY(1800deg)";
}
Found at: /
And in it, a coin spins. However, it only fires once. The first time I click it. It doesn't fire again after. Help!
So I made this,
var Coin = document.getElementById("coin");
Coin.onclick = function() {
Coin.style.webkitTransform = "rotateY(1800deg)";
Coin.style.MozTransform = "rotateY(1800deg)";
Coin.style.msTransform = "rotateY(1800deg)";
Coin.style.OTransform = "rotateY(1800deg)";
Coin.style.transform = "rotateY(1800deg)";
}
Found at: https://jsfiddle/dkjufqn0/
And in it, a coin spins. However, it only fires once. The first time I click it. It doesn't fire again after. Help!
Share Improve this question asked Jan 22, 2016 at 12:39 Dammy CheeDammy Chee 8510 bronze badges 2- 1 The event fires every time you click it, you should make the degree a variable and change it with every click. – A1rPun Commented Jan 22, 2016 at 12:43
- 2 It doesn't fires only once. It acutally fires every time you click it. It sets the rotation to 1800deg every time, that's it. So after the first time, you just can't notice the effect because the rotation is already set to 1800deg. – Jeremy Thille Commented Jan 22, 2016 at 12:57
2 Answers
Reset to default 8Every time you click the coin it remains at the 1800 degrees, to rotate it on each click you need to increment its degrees as in below example:
https://jsfiddle/dkjufqn0/1/
var Coin = document.getElementById("coin");
var degrees = 0;
Coin.onclick = function() {
degrees += 1800;
console.log(degrees)
Coin.style.webkitTransform = "rotateY(" + degrees + "deg)";
Coin.style.MozTransform = "rotateY(" + degrees + "deg)";
Coin.style.msTransform = "rotateY(" + degrees + "deg)";
Coin.style.OTransform = "rotateY(" + degrees + "deg)";
Coin.style.transform = "rotateY(" + degrees + "deg)";
}
body {
-webkit-transform: perspective(500px);
-webkit-transform-style: preserve-3d;
}
.coin {
background-image: url("http://coins.thefuntimesguide./images/blogs/presidential-dollar-coin-reverse-statue-of-liberty-public-domain.png");
background-size: 100% 100%;
border-radius: 100%;
height: 100px;
margin: 50px auto;
position: relative;
width: 100px;
-webkit-transition: 2s linear;
-webkit-transform-style: preserve-3d;
}
<div class="coin" id="coin"></div>
Are you sure about that? place a console.log(in that handler to check it)
I think your code is fine, except the rotateY calls seem to do nothing after the first click because you always set it to the same value.
本文标签: htmlJavascript onclick only works onceStack Overflow
版权声明:本文标题:html - Javascript onclick only works once - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745401660a2657068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论