2011-03-31 73 views
0

下面的代碼的cernain數一個div:廣場後的圖像

var images = [/*array with images*/]; 
var nofImages = images.length; 

$(document).ready(function(){ 

    var docheight = $('.main_insidem').height();  
    var docwidth = $('.main_insidem').width(); 
    var he = Math.round((docheight/120)); 
    var wi = Math.round((docwidth/115)); 
    var total = (he*wi); 

    var container = document.getElementById('slidesContainer'); 
    for (var i=0;i<nofImages;i++) { 
     var slide = document.createElement('div'); 
     slide.className="slide"; 

     var img = document.createElement('img'); 
     img.src=images[i]; 
     img.className="image"+i; 

     slide.appendChild(img); 
     container.appendChild(slide); 
    } 

}) 

表演:

<div class='main_insidem' style="height:500px;"> 
    <div id="slidesContainer"> 
     <div class="slide"><img src="1.png" class="image0"></div> 
     <div class="slide"><img src="2.png" class="image1"></div> 
     <div class="slide"><img src="3.png" class="image2"></div> 
     <div class="slide"><img src="4.png" class="image3"></div> 
     . 
     . 
     . 
     . 
    </div> 
</div> 

但我需要:

<div class='main_insidem' style="height:500px;"> 
    <div id="slidesContainer"> 
     <div class="slide"><img src="1.png" class="image0"> <img src="2.png" class="image1">.. and and so on until the value in var total is reached</div> 
     <div class="slide"><img src="..png" class="image.."> <img src="...png" class="image">.. and and so on until the value in var nofImages is reached</div> 
     . 
     . 
     . 
     . 
    </div> 
</div> 

我怎麼能這樣做?

非常感謝

+0

我不喜歡不明白你在var total_中的圖片數量 - 你能更具體地表達你想要達到的目標嗎? – Chris 2011-03-31 12:05:26

+0

您是否希望div與容器位於同一水平或嵌套? – McStretch 2011-03-31 12:05:37

+0

聽起來像你想在主循環內的另一個循環。有人會回來,並給你的解決方案,因爲沒有人在這裏讓人們學習...... – epascarello 2011-03-31 12:07:17

回答

1

試試這個,

var images = [/*array with images*/], 
    nofImages = images.length, 
    imagesPerDiv = 2; 

$(document).ready(function(){ 
    var docheight = $('.main_insidem').height(), 
     docwidth = $('.main_insidem').width(), 
     he = Math.round((docheight/120)), 
     wi = Math.round((docwidth/115)), 
     total = (he*wi), 
     container = $('#slidesContainer'); 

    loopCNT = Math.ceil(nofImages/imagesPerDiv); 

    for (var i=0;i<=loopCNT;i++) { 
     var slide = document.createElement('div'); 
     slide.className="slide"; 

     for (var j=i-1*imagesPerDiv;j<=imagesPerDiv;i++) { 
      var img = document.createElement('img'); 
      img.src=images[j]; 
      img.className="image"+j; 
      slide.appendChild(img); 
      j++; 
     } 
     container.appendChild(slide); 
    } 
}) 

你也可以修剪了很多的脂肪,而且能使「適當」使用jQuery,縮短了整個事情到以下:

var images = [/*array with images*/], 
    nofImages = images.length, 
    imagesPerDiv = 2; 

$(function() { 
    loopCNT = Math.ceil(nofImages/imagesPerDiv); 
    for (var i=0;i<=loopCNT;i++) { 
     var slide = $('<div />', { class: 'slide' }); 

     for (var j=i-1*imagesPerDiv;j<=imagesPerDiv;i++) { 
      $('<img />', { class: "image"+j, src: images[j] }).appendTo(slide); 
      j++; 
     } 
     $('#slidesContainer').append(slide); 
    } 
}) 
+0

它適合你嗎?它在這裏凍結Firefox和Chrome :( – user638009 2011-03-31 13:46:54

+0

j ++在第二個循環中,它的工作原理!!謝謝! – user638009 2011-03-31 14:23:06

0

在此請看:http://jsfiddle.net/AfNUS/2/

它不是很正確,它會在第一組圖像之前創建一個空白div它會做你所需要的,如果你可以計算出它爲什麼要添加空白div,那麼它將是完美的:)

+0

對不起沒有注意到你有一個有效的解決方案。 – 2011-03-31 15:12:06