自分のスライドショーを作成しようとしています。次のコードは、ある画像から別の画像にフェードインします。img1.jpgからimg4.jpgに切り替えたいのですが、変更するたびに一時停止する方法がわかりません。
i++;
temp = "img"+i+".jpg";
$("#img").fadeOut(function() {
$(this).load(function() { $(this).fadeIn(); });
$(this).attr("src", temp);
});
更新:コードを次のように変更しました。John Bokerのアドバイスにより、画像の名前をimg0、img1、img2、img3に変更しました。1枚目、2枚目、3枚目の画像に行きます。何か案は?
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
var temp="";
function slideshow(i)
{
temp = "img"+i+".jpg";
$("#img").fadeOut(function() {
$(this).load(function() {
$(this).fadeIn();
// set a timeout of 5 seconds to show the next image;
setTimeout(function(){ slideshow((i+1)%4); }, 5000);
});
$(this).attr("src", temp);
});
}
slideshow(0);
});
</script>
</head>
<body>
<img src="img1.jpg" id="img"/>
</body>
</html>