2012-08-14 4 views
0

이미지 갤러리의 모든 이미지를 미리로드하려고 시도하고 멋지면서도 간단한 코드 조각 (첫 번째 코드 샘플)을 발견했습니다.루프를 통해 배열 내용?

가능한 한 동적 인 갤러리를 만들고 싶습니다. 모든 이미지를 미리 불러올 수 있도록 갤러리의 모든 이미지를 반복하는 방법이 있습니까?

먼저 참조 용으로 모든 이미지를 미리로드하는 코드입니다. 두 번째는 내가 노력하고있는 것입니다.

// code for preloading images 
var images = [ 
    'bigPics/1.jpg', 
    'bigPics/2.jpg' 
]; 

$(images).each(function() { 
    var image = $('<img />').attr('src', this); 
}); 

.

// code I'm trying to re-work 

    // this give me the number of images in the gallery 
    var numberOfChildren = $(".thumb").length; 

// then I want to loop through all of the images that make up the array as above and output??? 
    for (var i=0; i<numberOfChildren; i++) 
     { 
     var images = [ 'bigPics/' + i + '.jpg' ]; 
     } 

    $(images).each(function() { 
     var image = $('<img />').attr('src', this); 
    }); 

답변

1

방법에 대한 ...

$('.thumb').each(function (i) { 
    $('<img>').attr('src', 'bigPics/' + i + '.jpg'); 
}); 
+0

좋은 일 SIME이 완벽하게 작동합니다! – user1563944

관련 문제