<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button id="button">Start Game</button>
<img src="https://www.chelanfresh.com/wp-content/uploads/2018/11/apple.png" id="apple">
</body>
</html>
#button {
min-width: 300px;
min-height: 300px;
color: #313133;
}
#apple {
height: 38%;
width: 10%;
margin-top: 27%;
margin-left: 19%;
visibility: hidden;
animation-name: appleSlide;
animation-duration: 3s;
animation-iteration-count: 1;
animation-direction: normal;
animation-play-state: paused;
animation-delay: 1s;
}
@keyframes appleSlide {
0% {margin-left: -50%;}
100% {margin-left: 19%}
}
let apple = document.getElementById("apple")
let button = document.getElementById("button")
function activateAnimations() {
apple.style.animationPlayState = "running";
apple.style.visibility = "visible";
}
button.addEventListener("click", activateAnimations)
我的动画基本上是隐藏或不可见的,但是一旦用户单击按钮,图片就会出现并滑动到特定位置。香港专业教育学院一直在摆弄animationPlayState无济于事,动画永远不会激活。
答案 0 :(得分:2)
apple.document.getElementById
)。activateAnimations
事件。这是您的代码的修补程序:
window.addEventListener("load", function() {
let apple = document.getElementById("apple")
let button = document.getElementById("button")
function activateAnimations() {
apple.style.animationPlayState = "running";
apple.style.visibility = "visible";
}
button.addEventListener("click", activateAnimations)
});
#button {
min-width: 300px;
min-height: 300px;
color: #313133;
}
#apple {
height: 38%;
width: 10%;
margin-top: 27%;
margin-left: -50%;
visibility: hidden;
animation-name: appleSlide;
animation-duration: 3s;
animation-iteration-count: 1;
animation-direction: normal;
animation-play-state: paused;
animation-delay: 1s;
animation-fill-mode: forwards;
}
@keyframes appleSlide {
0% {margin-left: -50%;}
100% {margin-left: 19%}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button id="button">Start Game</button>
<img src="https://www.chelanfresh.com/wp-content/uploads/2018/11/apple.png" id="apple">
</body>
</html>