在JS中使用单击功能触发键帧
因此,我正在尝试使用HTML,CSS和JS使用UI创建一个岩石剪刀游戏应该开始在屏幕上移动,但就我而言,它无能为力。
HTML:
<div class="game-screen-elements">
<h2 class="choice">Make your choice</h2>
<div class="fist-container">
<div class="fist-left"></div>
<div class="fist-right"></div>
</div>
<div class="pick">
<button class="rock" onclick="click()">rock</button>
<button class="paper">paper</button>
<button class="scissor">scissors</button>
</div>
</div>
</div>
CSS:
.fist-container{
display: flex;
width: 100%;
justify-content: space-around;
}
.fist-right, .fist-left{
background-image: url(assets/rock.png);
width: 300px;
height: 300px;
background-size: cover;
}
.fist-left{
transform: rotateY(180deg);
}
@keyframes fistLeft {
0% {
transform: rotateY(180deg) translateY(0px);
}
15% {
transform: rotateY(180deg) translateY(-50px);
}
/*25% {
transform: rotateY(180deg) translateY(0px);
} */
35% {
transform: rotateY(180deg) translateY(-50px);
}
50% {
transform: rotateY(180deg) translateY(0px);
}
65% {
transform: rotateY(180deg) translateY(-50px);
}
75% {
transform: rotateY(180deg) translateY(0px);
}
85% {
transform: rotateY(180deg) translateY(-50px);
}
100% {
transform: rotateY(180deg) translateY(0px);
}
}
@keyframes fistRight {
0% {
transform: translateY(0px);
}
15% {
transform: translateY(-50px);
}
/*25% {
transform: translateY(0px);
} */
35% {
transform: translateY(-50px);
}
50% {
transform: translateY(0px);
}
65% {
transform: translateY(-50px);
}
75% {
transform: translateY(0px);
}
85% {
transform: translateY(-50px);
}
100% {
transform: translateY(0px);
}
}
JS:
const playerHand = document.querySelector('.fist-left');
const rockChoice = document.querySelector('rock');
click = function(){
rockChoice.addEventListener('click', function(){
playerHand.style.animation = 'fistLeft 2s ease';
});
};
So I'm trying to create a rock paper scissors game with UI using html, css and JS and I got stuck on a problem, whenever the user chooses the rock by clicking the rock button, the rock div that contains the image with a fist should start to move on the screen but in my case it doesn't do anything.
The html:
<div class="game-screen-elements">
<h2 class="choice">Make your choice</h2>
<div class="fist-container">
<div class="fist-left"></div>
<div class="fist-right"></div>
</div>
<div class="pick">
<button class="rock" onclick="click()">rock</button>
<button class="paper">paper</button>
<button class="scissor">scissors</button>
</div>
</div>
</div>
The css:
.fist-container{
display: flex;
width: 100%;
justify-content: space-around;
}
.fist-right, .fist-left{
background-image: url(assets/rock.png);
width: 300px;
height: 300px;
background-size: cover;
}
.fist-left{
transform: rotateY(180deg);
}
@keyframes fistLeft {
0% {
transform: rotateY(180deg) translateY(0px);
}
15% {
transform: rotateY(180deg) translateY(-50px);
}
/*25% {
transform: rotateY(180deg) translateY(0px);
} */
35% {
transform: rotateY(180deg) translateY(-50px);
}
50% {
transform: rotateY(180deg) translateY(0px);
}
65% {
transform: rotateY(180deg) translateY(-50px);
}
75% {
transform: rotateY(180deg) translateY(0px);
}
85% {
transform: rotateY(180deg) translateY(-50px);
}
100% {
transform: rotateY(180deg) translateY(0px);
}
}
@keyframes fistRight {
0% {
transform: translateY(0px);
}
15% {
transform: translateY(-50px);
}
/*25% {
transform: translateY(0px);
} */
35% {
transform: translateY(-50px);
}
50% {
transform: translateY(0px);
}
65% {
transform: translateY(-50px);
}
75% {
transform: translateY(0px);
}
85% {
transform: translateY(-50px);
}
100% {
transform: translateY(0px);
}
}
The JS:
const playerHand = document.querySelector('.fist-left');
const rockChoice = document.querySelector('rock');
click = function(){
rockChoice.addEventListener('click', function(){
playerHand.style.animation = 'fistLeft 2s ease';
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您的工作JavaScript代码:
说明:您的单击函数所做的只是注册事件列表,而不一定要执行动画代码,因此未触发事件。分离的单击函数现在实际运行了负责动画的代码,因为它已通过 .Rock 类在您元素上的任何单击事件进行注册。还要确保将JS代码放在车身标签的底部(内部关闭标签),如果您将其用作外部脚本。
Here is your working Javascript code:
Explanation: What your click function does is simply registering the event-listener and not necessarily executing the animation code, hence the event is not triggered. The separated click function now actually runs the code responsible for animation since it has been registered for any click events on your element with .rock class. Also make sure you are placing the JS code at the bottom of the body tag(inside closing body tag) if you are using it as external script.