2010-10-15 120 views
3

我對JavaScript很陌生,我需要一個非常簡單的腳本來在一個循環中慢慢地淡入淡出圖像。任何幫助將非常感激。Javascript淡入淡出圖像

+5

如果你能抵抗jQuery的警報器歌曲這裏是一個DOM方法:http://stackoverflow.com/questions/2207586 – Mic 2010-10-15 16:18:10

回答

4

最簡單的方法是使用jQuery:

<img src="..." id="myImage"> 


<script type="text/javascript"> 
jQuery(function(){ 

    // Fade In 
    $("#myImage").fadeIn(); 

    // Fade Out 
    $("#myImage").fadeOut(); 

}); 
</script> 

文檔: http://api.jquery.com/fadeIn/

您還可以通過傳遞參數

// predefined slow (200ms) 
$("#myImage").fadeOut("slow"); 

// predefined fast (600ms) 
$("#myImage").fadeOut("fast"); 

// 1500 ms 
$("#myImage").fadeOut(1500); 

更新制作變更衰落期一個循環:

function fadeIn() 
{ 
    $(this).fadeIn(fadeOut); 
} 

function fadeOut() 
{ 
    $(this).fadeOut(fadeIn); 
} 

fadeIn.call($("#myImage"));