gpt4 book ai didi

javascript - 如何在单击某个按钮时激事件画?

转载 作者:行者123 更新时间:2023-11-30 23:56:22 24 4
gpt4 key购买 nike


<!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,但无济于事,并且动画从未激活。

最佳答案

  1. 您的代码中有些内容实际上没有意义(例如 apple.document.getElementById)。
  2. 您应该将苹果的原始位置添加到元素(而不仅仅是动画关键帧)。
  3. 该按钮没有 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>

关于javascript - 如何在单击某个按钮时激事件画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61021935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com