1

I'm new to jquery and javascript so sorry if this question seems stupid.

I try to load in several div the content (images) of corresponding folders to make a gallery.

<div id="menu">
<div class="gallery" id="div0">ok0</div>
<div class="gallery" id="div1">ok1</div>
<div class="gallery" id="div2">ok2</div>
<div class="gallery" id="div3">ok3</div>
<div class="gallery" id="div4">ok4</div>
<div class="gallery" id="div5">ok5</div>        
</div>

<script>
var $container = $("#menu div.gallery");    
  $container.each(function(){
    var $n = $(this).attr('id');
    tl = 0;

    var img = ["0001","0002","0003","0004","0005","0006","0007","0008","0009"];
    $.each(img,function (i, v) {
    $container.append('<div class="gallery_img" style="top:' + tl + 'px; left:' + tl + 'px; background-image:url(imgsmall/'+ $n +'/'+ v +'.jpg);"></div>');
      tl += 8;});           
});

$container.width(tl + 200).height(tl + 200).find("div").mouseover(
    function () {
    $(this).topZIndex();
    }
);  
</script>

Every div id correspond to the name of the folder but i can't find the way to upload the content of the good folder in the good div because of the $container.each() function and the $.each() one. this way it uploads the content of all folders in every div.

Also how can i replace this array

  var img = ["0001","0002","0003","0004","0005","0006","0007","0008","0009"];

with something neat?

Amandine
  • 11
  • 3
  • " the good file in the good div" Could you elaborate? – A. Wolff Sep 23 '13 at 09:08
  • i should have said: "Every div id correspond to the name of the folder but i can't find the way to upload the content of the good folder in the good div" Every div has to show a bunch of images which are in a folder with the same name has the div id. – Amandine Sep 23 '13 at 09:18

1 Answers1

0

this way it uploads all the files in each div

It's because for each <div> you loop through all images to append them to DOM.

If <div> and images are in the same order, you can do :

var img = ["0001","0002","0003","0004","0005","0006","0007","0008","0009"];
$container.each(function(index, el){
    var $n = el.attr('id');
    tl = 0;

    var currentImg = img.splice(index, 1);

    el.append('<div class="gallery_img" style="top:' + tl + 'px; left:' + tl + 'px; background-image:url(imgsmall/'+ $n +'/'+ currentImg +'.jpg);"></div>');

      tl += 8;           
});
Elorfin
  • 2,487
  • 1
  • 27
  • 50
  • what do you mean by "If
    and images are in the same order"? I tried this but the .splice() and the .each() doesn't work. I don't know if my explanation is clear enough
    – Amandine Sep 23 '13 at 10:11
  • @Amandine : if you want `img[0001]` go into `
    `, `img[0002]` into `
    `, etc.
    – Elorfin Oct 01 '13 at 09:44