简体   繁体   English

如何在JavaScript中更改要应用于其他功能的按钮的颜色?

[英]How can I change the color of a button within JavaScript to be applied into another function?

I am trying to create a button effect into javascript to be called into another function. 我试图在javascript中创建一个按钮效果,以调用另一个函数。 When the button is clicked it will display another colour then it will come back to its original state (colour). 单击该按钮时,它将显示另一种颜色,然后将返回其原始状态(颜色)。 It could indeed be done with :active in css but I will not be able to use the effect into another function. 确实可以在CSS中使用:active来完成此操作,但是我将无法在其他函数中使用该效果。

The final usage of the code is for the Simon game project in which will be randomly created sequences where players will follow. 该代码的最终用途是用于Simon游戏项目,在该项目中将随机创建序列,玩家将跟随该序列。

I already have the sound played as well as changing the colour of each button but am not being able to bring back buttons to its original state (colour) after clicking. 我已经播放了声音并更改了每个按钮的颜色,但是单击后无法将按钮恢复到其原始状态(颜色)。

let blueBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
let redBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
let yellowBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
let greenBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');

// VARIABLES - DOM QUERIES

const btnBlue = document.querySelectorAll("#btnBlue");
const btnGreen = document.querySelectorAll("#btnGreen");
const btnRed = document.querySelectorAll("#btnRed");
const btnYellow = document.querySelectorAll("#btnYellow");

$(btnBlue).click(function() {
    $(this).css('background-color', '#00FFFF');
    blueBtnAudio.play();
    if($(btnBlue).css('background-color') == '#00FFFF'){
      $(btnBlue).css('background-color', 'blue')
    }
  });

I suppose you want the button to change its color then come back to its original color when the sound is finished playing? 我想您希望按钮更改其颜色,然后在声音播放完毕后恢复其原始颜色?

You can do this by two ways, first store the button's original color in a variable, then change its color and play the sound and finally set a timeout of 1 second then change the button's color to its original one like so : 您可以通过两种方式做到这一点,首先将按钮的原始颜色存储在变量中,然后更改其颜色并播放声音,最后将超时设置为1秒,然后将按钮的颜色更改为其原始颜色,如下所示:

 let blueBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'); let redBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3'); let yellowBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3'); let greenBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3'); // VARIABLES - DOM QUERIES const btnBlue = $("#btnBlue"); const btnGreen = $("#btnGreen"); const btnRed = $("#btnRed"); const btnYellow = $("#btnYellow"); $(btnBlue).click(function() { var originalColor = $(this).css('background-color'); $(this).css('background-color', '#00FFFF') var newColor = $(this).css('background-color'); if (originalColor != newColor) { blueBtnAudio.play(); setTimeout(function() { $(btnBlue).css('background-color', originalColor); }, 1000); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="btnBlue"> btnBlue </button> 

or you can add an event listener for when the audio has ended playing and then applying your button its original color (this solution is better as it is dependent by the length of the audio and not the manual timeout we set in the first solution) : 或者您可以为音频结束播放时添加事件侦听器 ,然后将其原始颜色应用到按钮上(此解决方案更好,因为它取决于音频的长度,而不是我们在第一个解决方案中设置的手动超时):

 let blueBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'); let redBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3'); let yellowBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3'); let greenBtnAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3'); // VARIABLES - DOM QUERIES const btnBlue = $("#btnBlue"); const btnGreen = $("#btnGreen"); const btnRed = $("#btnRed"); const btnYellow = $("#btnYellow"); $(btnBlue).click(function() { var originalColor = $(this).css('background-color'); $(this).css('background-color', '#00FFFF') var newColor = $(this).css('background-color'); if (originalColor != newColor) { blueBtnAudio.play(); blueBtnAudio.addEventListener('ended', function() { $(btnBlue).css('background-color', originalColor); }); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="btnBlue"> btnBlue </button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM