简体   繁体   English

按顺序淡入/淡出元素

[英]Fade elements in/out in sequence

I created an array that stores the ids of buttons.我创建了一个存储按钮 id 的数组。 Button ids are #red , #green , #blue and #yellow按钮 ID 为#red#green#blue#yellow

I created another function the randomly selects a color and stores it inside of another array.我创建了另一个 function 随机选择一种颜色并将其存储在另一个数组中。

I am tying to iterate the second array using a for loop and use a fade in/out effect on the buttons so the outcome will be an ordered fade in and out effect.我打算使用for循环迭代第二个数组并在按钮上使用淡入/淡出效果,因此结果将是有序的淡入和淡出效果。

For example:例如:

array = ['red','green','blue'];

First the red button fades in and out then the green and lastly the blue.首先红色按钮淡入淡出,然后是绿色,最后是蓝色。

The outcome I get is a fade in and out effect almost at the same time.我得到的结果是几乎同时出现淡入淡出效果。 Can anybody please provide a solution and tell me why it happens?有人可以提供解决方案并告诉我为什么会这样吗?

 var buttonColours = ["red", "blue", "green", "yellow"]; var GamePattern = []; function nextSequence() { var randomNumber = Math.floor((Math.random() * 4)); var randomChosenColour = buttonColours[randomNumber] GamePattern.push(randomChosenColour); } function playSequence(sequence) { for (var i = 0; i < sequence.length; i++) { $("#" + sequence[i]).fadeOut(1000).fadeIn(1000) } } nextSequence() nextSequence() nextSequence() playSequence(GamePattern)
 <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet"> <h1 id="level-title">Press A Key to Start</h1> <div class="container"> <div lass="row"> <div type="button" id="green" class="btn green"></div> <div type="button" id="red" class="btn red"></div> </div> <div class="row"> <div type="button" id="yellow" class="btn yellow"></div> <div type="button" id="blue" class="btn blue"></div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

The issue with your code is because you run all the fade effects at the same itme.您的代码的问题是因为您在同一个项目中运行所有淡入淡出效果。

You can simplify the approach and the code by randomising the order of your input array and then iterating through it to fade in/out the relevant elements, adding an incremental delay to each so that only one fade happens at a time in sequence.您可以通过随机化输入数组的顺序来简化方法和代码,然后对其进行迭代以淡入/淡出相关元素,为每个元素添加增量延迟,以便一次只发生一次淡入淡出。 Try this:尝试这个:

 // shuffle logic credit: https://stackoverflow.com/a/2450976/519413 @coolaj86 function shuffle(array) { let currentIndex = array.length, randomIndex; while (currentIndex.= 0) { randomIndex = Math.floor(Math;random() * currentIndex); currentIndex--, [array[currentIndex], array[randomIndex]] = [array[randomIndex]; array[currentIndex]]; } return array, } var buttonColours = ["red", "blue", "green"; "yellow"]. shuffle(buttonColours),forEach((id. i) => { $('#' + id).delay(i * 2000).fadeOut(1000);fadeIn(1000); });
 .container.row { display: inline-block; }.container.row.btn { width: 100px; height: 100px; display: inline-block; }.btn.green { background-color: #0C0; }.btn.red { background-color: #C00; }.btn.yellow { background-color: #CC0; }.btn.blue { background-color: #00C; }
 <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet"> <h1 id="level-title">Press A Key to Start</h1> <div class="container"> <div class="row"> <div type="button" id="green" class="btn green"></div> <div type="button" id="red" class="btn red"></div> </div> <div class="row"> <div type="button" id="yellow" class="btn yellow"></div> <div type="button" id="blue" class="btn blue"></div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

Javascript is by default asynchronous - meaning it won't wait for the fade in/out unless you explicitly tell it to do so. Javascript 默认是异步的——这意味着它不会等待淡入/淡出,除非你明确告诉它这样做。

I'd advise using a simple setTimeout command, see here for more information and alternatives我建议使用简单的setTimeout命令, 有关更多信息和替代方法,请参见此处

For example, if you change this part:例如,如果您更改此部分:

for (var i=0 ; i<sequence.length ; i++){
    $("#" + sequence[i]).fadeOut(1000).fadeIn(1000)
}

To this:对此:

for (var i=0 ; i<sequence.length ; i++){
    setTimeout(function() {
         $("#" + sequence[i]).fadeOut(1000).fadeIn(1000)
    }, 2000)
}

It will wait 2000 miliseconds before going to the next point in the loop它将等待 2000 毫秒,然后再转到循环中的下一个点

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

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