同时fadeIn和fadeOut

时间:2012-05-07 20:07:36

标签: javascript jquery html css html5

以下代码有效,但新图像在前一个淡出之前不会淡入。我希望他们同时使用fadeIn和fadeOut,使其看起来好像新图像在旧图像上消失。

HTML

<img id="BiblesandCommentaries_bg" src="_\images\backgrounds\BiblesandCommentaries_bg.jpg"/>
<img id="EnquirersLibrary_bg" src="_\images\backgrounds\EnquirersLibrary_bg.jpg"/>
<img id="NewBelievers_bg" src="_\images\backgrounds\NewBelievers_bg.jpg"/>
<img id="ChristianLiving_bg" src="_\images\backgrounds\ChristianLiving_bg.jpg"/>
<img id="FamilyLibrary_bg" src="_\images\backgrounds\FamilyLibrary_bg.jpg"/>
<img id="YoungAdultLibrary_bg" src="_\images\backgrounds\YoungAdultLibrary_bg.jpg"/> 
<img id="WorshipLibrary_bg" src="_\images\backgrounds\WorshipLibrary_bg.jpg"/>
<img id="BibleTeachings_bg" src="_\images\backgrounds\BibleTeachings_bg.jpg"/>
<img id="Leadership_bg" src="_\images\backgrounds\Leadership_bg.jpg"/>
<img id="SchoolofChrist_bg" src="_\images\backgrounds\SchoolofChrist_bg.jpg"/>

JS

$('#sidebar a').click(function() {
    $('#BiblesandCommentaries_bg, #EnquirersLibrary_bg, #NewBelievers_bg, #ChristianLiving_bg, #FamilyLibrary_bg, #YoungAdultLibrary_bg, #WorshipLibrary_bg, #BibleTeachings_bg, #Leadership_bg, #SchoolofChrist_bg').fadeTo("slow", 0);
    bgImage = $(this).attr('href')+'_bg';
    $(bgImage).fadeTo("slow", 1);
});

1 个答案:

答案 0 :(得分:2)

当你在元素上调用fadein动画时,另一个fadeout动画仍然在执行,所以尽量不要淡出应该淡入的元素。代码是这样的:(我添加了.not(bgImage)部分它。)

$('#sidebar a').click(function() {
     var bgImage = $(this).attr('href')+'_bg';    
     $('#BiblesandCommentaries_bg, #EnquirersLibrary_bg, #NewBelievers_bg, #ChristianLiving_bg, #FamilyLibrary_bg, #YoungAdultLibrary_bg, #WorshipLibrary_bg, #BibleTeachings_bg, #Leadership_bg, #SchoolofChrist_bg').not(bgImage).fadeTo("slow", 0);    
     $(bgImage).fadeTo("slow", 1);
});