【发布时间】:2021-04-21 16:56:40
【问题描述】:
我在选择中有 3 个值:石头、纸、剪刀。
当我第一次在选择中选择一个值(石头、纸或剪刀)时,onChange 事件触发,@keyframes 动画适用于所有 3 个值。
但我第二次选择任何值时,动画不再起作用。 我不明白为什么。
代码是这样的: jsfiddle.net/9efvxph3/83/
body {
background: radial-gradient(gold, goldenrod);
font-family: monospace;
text-align: center;
font-size: 1.5rem;
}
@keyframes example {
0% {
transform: scale(1);
}
100% {
transform: scale(1.3);
}
}
img {
padding: 10px;
height: 100px;
width: auto;
}
img:hover {
transform: scale(1.4);
transition: all 1s;
}
<select id="choice" onchange="winner()">
<option value="" disabled selected>choose</option>
<option value="rock">rock</option>
<option value="paper">paper</option>
<option value="scissors">scissors</option>
</select>
<img src="https://img.icons8.com/windows/2x/hand-rock.png" class="user-choice">
<img src="https://img.icons8.com/wired/2x/paper.png" class="user-choice">
<img src="https://img.icons8.com/ios/2x/hand-scissors.png" class="user-choice">
<h3 id="result"> </h3>
<script>
const type = ["paper", "scissors", "rock"];
function winner() {
var index = document.getElementById("choice").selectedIndex;
var userChooice = document.getElementById("choice").value;
var PcChooice = type[Math.floor(Math.random() * type.length)];
document.getElementsByClassName("user-choice").item(index - 1).style = "animation:example 0.5s alternate";
if (PcChooice == userChooice) {
document.getElementById("result").innerHTML = userChooice + " - " + PcChooice + ": You tied";
} else {
if ((userChooice == "rock") && (PcChooice == "scissors")) {
document.getElementById("result").innerHTML = userChooice + " - " + PcChooice + ": You win";
} else if ((userChooice == "paper") && (PcChooice == "rock")) {
document.getElementById("result").innerHTML = userChooice + " - " + PcChooice + ": You win";
} else if ((userChooice == "scissors") && (PcChooice == "paper")) {
document.getElementById("result").innerHTML = userChooice + " - " + PcChooice + ": You win";
} else {
document.getElementById("result").innerHTML = userChooice + " - " + PcChooice + ": You loose";
}
}
document.getElementById("choice").selectedIndex = 0;
};
</script>
【问题讨论】:
标签: javascript css css-animations onchange css-transforms