我已经对您的代码进行了一些更改,您可以在http://jsfiddle.net/24Qat/22/看到一个实时示例
我设置了一组图像:
var images = {};
images[0] = "http://seninsayfan.net/ext/avatar/img1.jpg";
images[1] = "http://seninsayfan.net/ext/avatar/img2.png";
images[2] = "http://seninsayfan.net/ext/avatar/img4.jpg";
images[3] = "http://seninsayfan.net/ext/avatar/image1.jpg";
images[4] = "http://seninsayfan.net/ext/avatar/image2.gif";
images[5] = "http://seninsayfan.net/ext/avatar/pic1.gif";
images[6] = "http://seninsayfan.net/ext/avatar/pic2.jpg";
images[7] = "http://seninsayfan.net/ext/avatar/pic3.jpg";
images[8] = "http://seninsayfan.net/ext/avatar/pic4.gif";
我保存图像索引:
var shown = [0,1,2,3,4,5];
var hidden = [6,7,8];
比我使用选择源和目标来进行交换:
$(document).ready(function() {
setInterval(function() {
// choose target index
var old_pic = Math.floor(Math.random() * 6);
// choose source index
var new_pic = Math.floor(Math.random() * 3);
$(".image-" + old_pic).fadeOut(500, function() {
$(this).find('img').attr("src", images[hidden[new_pic]]);
var temp = shown[old_pic];
shown[old_pic] = hidden[new_pic];
hidden[new_pic] = temp;
}).fadeIn(500);
}, 1000);
});
请注意,对于更精简的代码,我已将索引更改为从 0 开始。此外,我已删除最后 3 个图像(只需要 6 个容器),因此 html 应为:
<div class="image image-0" ><img src="http://seninsayfan.net/ext/avatar/img1.jpg"/></div>
<div class="image image-1" ><img src="http://seninsayfan.net/ext/avatar/img2.png"/></div>
<div class="image image-2" ><img src="http://seninsayfan.net/ext/avatar/img4.jpg"/></div>
<div class="image image-3" ><img src="http://seninsayfan.net/ext/avatar/image1.jpg"/></div>
<div class="image image-4" ><img src="http://seninsayfan.net/ext/avatar/image2.gif"/></div>
<div class="image image-5" ><img src="http://seninsayfan.net/ext/avatar/pic1.gif"/></div>