我想创建一个循环,以加载数组中某个页面的所有图像。它是这样工作的:我有一个包含URL的数组。我想将所有这些页面的图像加载到某个div,但是jQuery load();
函数仅从array
的一个页面加载图像,并且它是随机进行的。因此,每次我得到的图像都不同,但只有一页。
var pages = ["artificial-waterfall.htm", "artificial-waterfall01.htm", "artificial-waterfall02.htm", "artificial-waterfall04.htm", "artificial-waterfall05.htm", "artificial-waterfall07.htm", "artificial-waterfall08.htm"];
pages.forEach(function(value, i) {
$('.slides').append('<div class="slide"></div>')
$('.slide').load('/russian/wild-stonenew/' + pages[i] + " .fancybox", function(){
$('.slide .fancybox').attr('data-fancybox', 'gallery1');
});
});
答案 0 :(得分:0)
根据我的评论-加载不是随机的,它仅取决于加载页面所花费的时间,因为每个页面中的加载都是异步的。如果要使其同步,则需要将其移出每个循环,然后在第一页完成后使用回调函数加载下一页。
代替在每个循环中进行加载,您可以执行以下操作:
var pages = ["artificial-waterfall.htm", "artificial-waterfall01.htm", "artificial-waterfall02.htm", "artificial-waterfall04.htm", "artificial-waterfall05.htm", "artificial-waterfall07.htm", "artificial-waterfall08.htm"];
var currentPage = 0; // global counter
var $slidesContainer = $('.slides'); // slide container
addSlide(); // set loop going
function addSlide() {
$slide = $('<div class="slide"></div>'); // create new slide
$slidesContainer.append($slide); // append to container
$slide.load('/russian/wild-stonenew/' + pages[currentPage] + " .fancybox", function() { // load images into slide
$slide.find('.fancybox').attr('data-fancybox', 'gallery1'); // do your fancybox thing
currentPage++; // increment page index
if (currentPage < pages.length) { // load next slide if there is one
addSlide();
}
});
}
答案 1 :(得分:0)