这里是一个JSfiddle为什么JQuery要求我显式地将排队的图像淡出?
演示一些简单的JS函数的排队。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<style>
.div1 {
height:300px;
width:20%;
background-color: red;
float:left;
}
</style>
<meta charset="ISO-8859-1">
<title>Test</title>
</head>
<body>
<div class="div1">div1</div>
<img class="image" style='width: 30%; height: 30%' src='https://www.google.com/images/srpr/logo11w.png'>
<button type="button" onclick="doStuff()" name="do">Do it</button>
</body>
<script>
function doStuff()
{
var body = $('body');
body.clearQueue();
body.queue(function(){
console.log('here');
body.css("background-color", "yellow");
});
var div1 = $('.div1');
div1.clearQueue();
div1.queue(function(){
div1.css("background-color", "green");
});
var image = $('.image');
image.clearQueue();
image.queue(function(){
$(this).fadeOut(3000)});
//image.dequeue(); //image doesn't fade without this step
}
</script>
</html>
请注意,单击按钮时,正文和div1颜色会按预期更改。但是,除非我取消注释Google图像淡出的出队声明,否则图像不会褪色。我很好奇,为什么我不必明确地将前两个操作出列,以使它们生效,但是随着图像淡入淡出,我必须调用出列方法。
你能解释一下你试图通过通过动画队列明确地做这些操作来实现呢?比如,为什么不直接执行'image.fadeOut(3000);'? – Pointy
@有点肯定,这将工作,但我正在一个网页上工作,在稍后执行它们之前,我必须排队几个操作。在设计过程中,我注意到了这种行为,在继续之前需要了解它。 – swingMan