2011-12-12 101 views
0

這是關係到我的其他職位,但它是一個不同的場景。jquery懸停的時間延遲顯示圖像下一個

當我將鼠標懸停在框上時,我想要顯示圖像2秒鐘,然後在2秒後顯示下一張圖像。當他們徘徊的時候,我希望他們都消失(沒有淡入或了slideDown等)

// html 

<style type="text/css"> 
#date_1 img{display:none} 
</style> 

<div id="date_1"> 
<img src="2secondanimation.gif" id="magicimage_1" /> 
<img src="fixedimage.jpg" id="magicimage_1_fixed" /> 
</div> 

和JS:

// jquery (UPDATED CODE) 

$("#date_1").hover(function() { 
    // show image for 2 seconds 
    $("#magicimage_1").show(2000); 
    $("#magicimage_1_fixed").show(); 
}, function() { 
    // remove both above classes 
    $("#magicimage_1").hide(); 
    $("#magicimage_1_fixed").hide(); 
}); 
+0

你已經用jQuery標記了你的問題,但是你並沒有在你的代碼中好好利用它。像document.getElementById這樣的東西在jQuery中有相同的地方。 –

+0

,因爲我不知道顯示隱藏和取消隱藏的命令,我只知道顯示項目的slideDown和fadeIn。 – TheBlackBenzKid

+1

@TheBlackBenzKid'$('#myId')。show()'和'$('#anotherId')。hide()' – Petah

回答

1

嘗試是這樣的:

http://jsfiddle.net/gYmvN/

$("#date_1").hover(function() { 

    $("#img1").show(); 
    $("#img2").delay(2000).show(true); 

}, function(){ 
    $("#img1").hide(); 
    $("#img2").hide(); 

}); 

I相信這是你所要求的。


編輯

與新代碼更新來重新啓動GIF

http://jsfiddle.net/gYmvN/4/

+0

會有一種方法來播放懸停的動畫GIF,然後當MouseOut隱藏類,但當他們回到動畫GIF它再次從幀1播放相同的GIF? GIFS對緩存不好 – TheBlackBenzKid

+1

@TheBlackBenzKid是的,你可以預緩存你的gif圖像,然後重新加載你的圖像中的src。因爲它被緩存了,所以它會很快重新加載,看起來就像剛剛在第1幀開始的那樣。看看http://stackoverflow.com/questions/3191922/restart-an-animated-gif-from-javascript-without -reloading-the-image,你可能想使用$(「#magicimage_1」)。attr(...) – Matt

+0

我實際上發佈了另一個問題來幫助用戶更好。 http://stackoverflow.com/questions/8474199/reload-animated-gif-cache-each-jquery-hover – TheBlackBenzKid

2
$("#date_1").hover(function() { 
// show image for 2 seconds 
$("#magicimage_1").delay(2000).css({'display':'block'}); 
// after two seconds show this and do not change 
$("#magicimage_1_fixed").delay(2000).css({'display':'block'}); 
}, function() { 
    // remove both above classes 
    $("#magicimage_1").css({'display':'none'}); 
    $("#magicimage_1_fixed").style.display="none"; 
});