我正在尝试使用<delay>
来延迟事件,例如更改背景颜色。我希望事件能够遵循我想要的延迟时间,但结果表明它们并不是我想要的顺序。我期待第一个在1秒内变红。然后第二个在2秒内变红。然后第三个在0.8秒内变红。我不知道如何让它们变成不同的颜色。 非常感谢您的帮助。延迟一个事件,如更改div的背景颜色
$(document).ready(function(){
var delayTime = [1000, 2000, 800];
var bcolor = ['red','blue','green'];
var i = 0;
$('#play').click(function(){
$('div').each(function(){
$(this).delay(delayTime[i]).queue(function(next){
$(this).css('background','red'); \t \t \t \t \t \t \t \t
next();
});
i++;
}); // end of each
});
}); // end ready
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>
<div id="red" style="width:100px; height: 100px; background-color: white" ></div>
<div id="blue" style="width:100px; height: 100px; background-color: white"></div>
<div id="green" style="width:100px; height: 100px; background-color: white"></div>
<button id="play">Play</button>
<h1 id="test"></h1>
重复@Gyryrillus答案在本质上 –