在JS中使用单击功能触发键帧

发布于 2025-02-08 20:42:05 字数 2451 浏览 2 评论 0原文

因此,我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网名女生简单气质 2025-02-15 20:42:05

这是您的工作JavaScript代码:

const playerHand = document.querySelector('.fist-left');
const rockChoice = document.querySelector('.rock');

rockChoice.addEventListener('click', click);
        

function click(){
    playerHand.style.animation = 'fistLeft 2s ease';
}

说明:您的单击函数所做的只是注册事件列表,而不一定要执行动画代码,因此未触发事件。分离的单击函数现在实际运行了负责动画的代码,因为它已通过 .Rock 类在您元素上的任何单击事件进行注册。还要确保将JS代码放在车身标签的底部(内部关闭标签),如果您将其用作外部脚本。

Here is your working Javascript code:

const playerHand = document.querySelector('.fist-left');
const rockChoice = document.querySelector('.rock');

rockChoice.addEventListener('click', click);
        

function click(){
    playerHand.style.animation = 'fistLeft 2s ease';
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文