2013-03-15 50 views
0

我在我的網站上有一些圖像,並且這個代碼我想隱藏它們,改變顯示的圖像並顯示它們,但是新圖像立即顯示。我不知道該怎麼辦。.delay()函數在attr()函數上不起作用

這是JavaScript:

$(document).ready(function() { 
    $('.show').click(function() { 
    $(this).removeClass("show").addClass("clickedShow"); 
    $('.show').animate({opacity: 0}, 1000); 
    $(this).delay(1000).animate({opacity: 0}, 1000); 

    $(this).animate({opacity: 1}, 1000).attr("src", "pic2.png"); 
    $('.show').delay(1000).animate({opacity: 1}, 1000).attr("src", "pic2.png"); 
    }); 
}); 
+3

這將解決您的問題:http://stackoverflow.com/questions/2807127/jquery-delay-doesnt-delay-attr-in-the-queue – Amrendra 2013-03-15 09:35:55

回答

4

delay僅適用於那些可以排隊動畫,attr不排隊,因此它不會受到delay。如果您想在動畫結尾處執行某些操作,請將該操作添加到animation回調中。

$(this).animate({opacity: 1}, 1000, function(){$(this).attr("src", "pic2.png")}); 
0

.delay()方法最適合延遲排隊的jQuery效果。因爲它是有限的 - 例如,它不提供取消延遲的方法。

因此,您可以嘗試使用排隊的jQuery效果,並在它們之間使用.delay(1000)。像:

$("button").click(function() { 
     $("div.first").slideUp(300).delay(800).fadeIn(400); 
     $("div.second").slideUp(300).fadeIn(400); 
    });</script>