简体   繁体   English

jQuery单击以显示图像,然后使其延迟后消失吗?

[英]JQuery clicking to show image, then making it disappear after delay?

I'm creating a simple game of Simon, and I want the easiest way to code where if one of the four buttons is clicked, it changes the image for a second or so (on mode), plays a sound, then goes back to its original color. 我正在创建一个简单的Simon游戏,我想以最简单的方式编写代码:如果单击四个按钮之一,它将更改图像一秒钟左右(在模式下),播放声音,然后返回到它的原始颜色。 Right now I've searched, but everything I've seen has to do with divs, or other types of questions. 目前,我已经进行了搜索,但是看到的所有内容都与div或其他类型的问题有关。

As of now the button changes to the on button image, plays the sound, but when I try any function to hide or fadeOut or anything of that sort, it goes to a strange greyish color, but does not disappear. 截至目前,按钮变为按钮上的图像,播放声音,但是当我尝试使用任何功能隐藏或淡出或类似功能时,它会变成一种奇怪的灰色,但不会消失。 I've tried to use attr() to set the image back to what it originally was with a delay in between, but that doesn't work, either. 我尝试使用attr()将图像设置回原来的状态,并且之间有一定的延迟,但这也不起作用。 The code is long, but I'll post what's relevant... 代码很长,但我会发布相关内容...

    <audio src="sounds/1.mp3" id ="snd1"></audio>
    <audio src="sounds/2.mp3" id ="snd2"></audio>
    <audio src="sounds/3.mp3" id ="snd3"></audio>
    <audio src="sounds/4.mp3" id ="snd4"></audio>
    <div id="juego">
        <div id="simon">
            <div id="c1"><img id ="c1i" src="imgs/d1.png"></div>
            <div id="c2"><img id ="c2i" src="imgs/d2.png"></div>
            <div id="c3"><img id ="c3i" src="imgs/d3.png"></div>
            <div id="c4"><img id ="c4i" src="imgs/d4.png"></div>
        </div>

and from my js file... 并从我的js文件中...

    $("#c1").on("click", function() {
            $("#c1i").attr("src", "imgs/1.png").hide("slow");
            $("#snd1")[0].play();
});

edit: I will also add that clicking the button as of now only lets me click it once to perform the function. 编辑:我还将补充一点,即从现在开始单击该按钮仅允许我单击一次以执行该功能。 After one click, it just greys out. 一键单击后,它只是变灰。 No color change, no sound played. 没有颜色变化,没有声音播放。 Mystifying to me... 令我迷惑的是...

try this : 尝试这个 :

$("#c1").on("click", function() {
       // change the image , and make sure this image path exists ...
      $("#c1i").attr("src", "imgs/1.png");
      // make it disappear and play the sound after 1 second
      setTimeout(function(){
        $("#c1i").hide();
        $("#snd1")[0].play();
      },1000);

});

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

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