2011-03-14 77 views
5

我是jQuery的新手,我正在尝试一个简单的程序。在这个程序中,我试图实现的是,当我点击div文本时,第一行将淡出,然后它会改变背景颜色,然后它会淡入淡出,这样会缓慢地改变颜色,但是这里发生了什么是当我点击文本的行突然改变颜色,然后淡出,然后淡入。所以我想知道是否有办法让执行淡出后执行颜色更改代码执行。在jquery中完成上一行后执行一行

我的程序:

 $(document).ready(function(){ 
      $("#firstDiv").click(function(){ 
       //     $(this).fadeOut(1000); 

       $(this).fadeOut(1000); 

       $(this).css("background-color", "aqua"); 

       $(this).fadeIn(1000); 


      }); 
     }); 
    </script> 
</head> 
<body> 
    <div id="firstDiv" style="background-color: yellowgreen"> 
     Click Me 
    </div> 
</body> 

回答

3

您可以使用淡出()这样的回调函数:

$(this).fadeOut(1000, function(){ 
    $(this).css("background-color", "aqua").fadeIn(1000); 
}); 
4

你必须把颜色传递给fadeOut回调改变:

$(this).fadeOut(1000, function() { 
    $(this).css("background-color", "aqua"); 
}).fadeIn(1000); 

Everyt兴你在回调后执行fadeOut完成后执行。 fadeIn不会立即执行,因为效果方法自动为queued

:如果你是做昂贵的东西计算(即它需要时间)在回调并要拨打fadeIn只有在此之后完成,你必须把fadeIn回调太:

$(this).fadeOut(1000, function() { 
    $(this) 
     .css("background-color", "aqua") 
     .fadeIn(1000); 
}); 

否则fadeIn将在回调完成前被调用。


另外值得一读在这方面:Can somebody explain jQuery queue?

+0

为什么衰落链自动但是CSS变化没有? – poke 2011-03-14 09:37:47

+0

@poke:刚刚添加到我的答案;) – 2011-03-14 09:39:46

相关问题