我试图制作一个图像推子库,循环通过大量图像,然后重复。我已经成功地写出了它在图像中消失的部分,但我无法弄清楚如何重复它。我正在使用的当前代码只是无限加载,我想因为我以某种方式创建了一个无限循环,但我不知道如何或如何绕过它。
的jQuery
//create array with the images id tags
var img_arr = [
'#img1',
'#img2',
'#img3'
]
var i = 0;
arr_length = img_arr.length;
gal_repeat = img_arr.length - 1;
$(document).ready(function img_gallery() {
//loop through the fade animation until the end of the array
for (i = 0; i < arr_length; i++) {
$(img_arr[i])
.animate(
{ opacity: '1.0' }, 2000
);
$('.img').delay(1000).animate({opacity: 0.0}, 2000);
//THIS IS WHERE THE PROBLEM IS. if I comment out this 'if'
//statement, the gallery works fine but doesn't repeat
if (i == gal_repeat) {
i = 0;
}
}
})
答案 0 :(得分:0)
您可以将setInterval用于重复性内容:
//create array with the images id tags
var images = ['#img1', '#img2', '#img3'];
numberOfImages = images.length;
$(document).ready(function() {
var num = 0;
setInterval(function() {
num = (num + 1) % numberOfImages;
$(images[num]).animate({
opacity: '1.0'
}, 2000);
$('.img').delay(1000).animate({
opacity: 0.0
}, 2000);
}, 5000);
});