2017-07-24 36 views
0

我試圖在一行中顯示三個圖像。例如,我試圖讓頁面看起來像這樣(我手動繼續並輸入了所有我想要得到的html):http://imgur.com/AsizGyy使用jQuery來顯示圖像

但是當我嘗試使用jQuery將圖像追加到嘗試使它看起來像它在以前的屏幕截圖中,它顯示如下:http://imgur.com/Vkt4ljH

很明顯,這是不同於我希望它是如何。

這裏是我的代碼(HTML和jQuery的兩者)

<div class="w3-container" id="album-header" style="display: none;"> 
    <h4 class="w3-text-black"></h4> 
</div> 

<div class="w3-row-padding w3-margin-top" style="display: none;"> 
    <div class="w3-third"> 
     <div class="w3-card-2" id="palbums"> 
      <!-- image here --> 
     </div> 
    </div> 
</div> 

$('#album-name').on('change', function() { 
    // retrieve the images from the photo album selected 
    $.ajax({ 
     type: "POST", 
     url: "/members/profile/get-photos-from-album", 
     dataType: "json", 
     data: { 
      album_name: $('#album-name option:selected').val() 
     } 
    }).done(function(msg) { 
     if (msg.photos == undefined) { 
      $('#no-album-photos .w3-col m12').attr('style', 'display: initial;'); 
      $('#no-album-photos .w3-col m12 ').html("<p>No photos were found in the album</p>"); 

      return false; 
     } else { 
      $('#album-header').attr('style', 'display: initial;'); 

      $('#album-header h4').html("Photos in " + $('#album-name option:selected').text().split("_")[0] + ":"); 

      $('.w3-row-padding .w3-margin-top').attr('style', 'display: initial;'); 

      $.each(msg.photos, function(i, item) { 
       $('div.w3-third').find('.w3-card-2').append('<img src="<?php echo $this->basePath() . '/images/profile/' . $this->identity() . '/albums/'; ?>' + $('#album-name option:selected').val() + '/' + msg.photos[i] + '" style="width: 100%;">'); 
      }); 
     } 
    }).fail(function(msg) { 
     alert(msg); 
    }); 
}); 

我知道這個問題是$。每個函數中,但我就如何解決這一問題(虧損搜索任何問題後,或如何完成這個教程,但不幸的是沒有運氣)。

的HTML我想獲得這將是這樣的:

<div class="w3-row padding w3-margin-top"> 
    <div class="w3-third"> 
     <div class="w3-card-2"> 
      <!-- image here --> 
     </div> 
    </div> 

    <div class="w3-third"> 
     <div class="w3-card-2"> 
      <!-- image here --> 
     </div> 
    </div> 

    <div class="w3-third"> 
     <div class="w3-card-2"> 
      <!-- image here --> 
     </div> 
    </div> 
</div> 
<!-- after 3 in a row --> 
<!-- make a new row --> 
<div class="w3-row padding w3-margin-top"> 
    <div class="w3-third"> 
     <div class="w3-card-2"> 
      <!-- image here --> 
     </div> 
    </div> 

    <div class="w3-third"> 
     <div class="w3-card-2"> 
      <!-- image here --> 
     </div> 
    </div> 

    <div class="w3-third"> 
     <div class="w3-card-2"> 
      <!-- image here --> 
     </div> 
    </div> 
</div> 

任何幫助,將不勝感激。如果需要更多信息,我可以嘗試發佈更多信息。

謝謝!

+0

你確定你的服務器是不是3次返回同一張照片的名字嗎?因爲每個循環在它的表面上看起來都不錯 – ADyson

+0

你提到你想要的是行,但是你堆疊你的'divs'的方式,你將創建正確的結果列。 –

+0

不,這是返回所有圖像@ADyson – user2101411

回答

1

您正在堆砌您的DIV的這將創建列,以便您的代碼工作正常,這是你的HTML錯誤。你可以使用float: left做到這一點

<div class="w3-row padding w3-margin-top"> 
    <div class="w3-third" style="float: left"> 
     <div class="w3-card-2"> 
      Image1 
     </div> 
    </div> 

    <div class="w3-third" style="float: left"> 
     <div class="w3-card-2"> 
      Image2 
     </div> 
    </div> 

    <div class="w3-third" style="float: left"> 
     <div class="w3-card-2"> 
      Image3 
     </div> 
    </div> 
</div> 

FIDDLE

或CSS它

.w3-third { 
    float: left; 
    max-height: 200px; 
} 
img { 
    max-height: 150px; 
    padding-bottom: 10px; 
} 
+0

我試過了,這是輸出http://imgur.com/CxIwwqw 比我有更好的,但試圖去這樣的事情:http://imgur.com/AsizGyy – user2101411

+0

它看起來像你的圖像有不同的高度,這將做到這一點。你想要他們都是一樣的大小? –

+0

是的,我想他們都是相同的大小。我之前做過,班級相應地調整圖像,但我想在一行(或行)上三個,然後去另一行(如http://imgur.com/AsizGyy) – user2101411

0

嘗試使用$ .each函數更新行。看起來像 在div.w3-third內循環,而不是div.w3-行

$(this).append('<div class="w3-third"><div class="w3-card-2"><img src="<?php echo $this->basePath() . '/images/profile/' . $this->identity() . '/albums/'; ?>' + $('#album-name option:selected').val() + '/' + msg.photos[i] + '" style="width: 100%;"></div></div>'); 

雖然這不是構建HTML使用jQuery的最好的實現。

您不需要每次都有新的行。如果你的css class .w3-third我認爲是你行的三分之一。你必須像這樣

CSS

.w3-third { 
    width: 33.3%; 
    float: left; 
} 

設置圖像包裝

.w3-card-2 { 
     height: 240px; 
     margin-bottom: 15px; 
    } 

OR

.w3-card-2, .w3-card-2 img { 
     height: 240px; 
} 

希望這有助於高度。

+0

是的,看起來比我有更好的方式,但我試圖讓它看起來像這樣:http://imgur.com/AsizGyy而不是像這樣http://imgur.com/CxIwwqw 它的方式是這使得它看起來像http://imgur.com/AsizGyy中的那個,它在一行中有三個三分之三的列,然後它創建了一個新行。這有助於更好地解釋它嗎? – user2101411

+0

這意味着你必須在圖像包裝器和/或圖像本身的css中設置最小高度或高度。我已經更新了這個效果的答案。 – Oluwaseye

0
.w3-third { 
float: left; 
width:30%; 
max-height: 200px; 
pading:0; 
} 
img { 
width:100%; 
margin-left:0; 
margin-right:0; 
max-height: 150px; 
padding-bottom: 10px; 
} 
+0

你有什麼想法爲什麼前兩個圖像間隔正確,但其他人不是? http://imgur.com/zXgBMZF如果你能看到前兩名在第二排下方有合適的空間,其餘的則沒有。任何想法? – user2101411

+0

是的,這是因爲3張圖片的寬度超過了主div的寬度我更新了代碼,但它會留下正確的空間,您必須更正它 – Osama