2014-01-09 146 views
1

我正在使用下面的Java腳本代碼來創建幻燈片與圖像 img1.jpg,img1.jpg和img3.jpg。 但它在輸出中只顯示img1.jpg。請告訴問題。幻燈片javascript不工作

<html> 
<style> 
#slideshow{width:310;height:210;border-style:solid;} 
#slideshow>img{position:absolute;left:15;top:15} 
</style> 
<body> 
<div id="slideshow"> 
<img id="imge" src="img1.jpg" height="200" width="300"> 
</div> 
<script> 
var count=2; 
function mf() 
{ 
document.getElementById("imge").src="img"+count+".jpg"; 
document.getElementById("imge").height="200"; 
document.getElementById("imge").width="300"; 
if(count<3) 
count++; 
else 
count=1; 
setTimeout("mf()",3000); 
} 
</script> 
</body> 
</html> 
+0

你還沒有在任何地方調用'mf'函數。 – tewathia

+0

哦......我多麼傻......謝謝 – user2828552

回答

1

您需要以後開始你的腳本,用MF();

<script> 
    var count=2; 
    // if your script is at the bottom of the page, you can move 
    // these three lines out here 
    var elem = document.getElementById("imge"); 
    elem.height="200"; 
    elem.width="300"; 

    function mf() { 

     elem.src="img"+ (count + 1) +".jpg"; // add 1 to grab images 1 through 3 

     count = ((count + 1) % 3); // will loop from 0 to 2 and back again 
     setTimeout(mf,3000); 
    } 
    mf(); // kick off the first iteration of the slideshow 
</script> 
+0

哦......我多麼傻......謝謝 – user2828552

+0

如果我想在這個幻燈片中添加過渡風格,我該怎麼辦? – user2828552

+0

這取決於你想要做什麼。您可能需要查看css轉換:https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions?redirectlocale=zh-CN&redirectslug=CSS%2FTutorials%2FUsing_CSS_transitions您可以通過編程方式添加和刪除CSS類來獲得所需的效果。你知道你的目標是什麼瀏覽器嗎? – Gopherkhan