2017-08-12 106 views
0

我有問題,我不知道如何解決,這很難解釋,但我會嘗試,也有圖片來演示。浮動項目列表

我訂購清單有問題,當李項目的下一行在第二行上有很多上面的行和下面的行之間的空間時,我會沒有間距。

https://jsfiddle.net/52suh3pt/

  • 我試圖用浮動來解決:離開,但它的工作只針對一方。

看這張圖片,左邊是正確的,我想要的,右邊是目前。

enter image description here

HTML:

<div> 
<ul> 
<li><img src="https://cdn.pixabay.com/photo/2016/04/15/04/02/water-1330252_960_720.jpg" alt=""></li> 
<li><img src="https://image.freepik.com/free-vector/blue-background_1344-24.jpg" alt=""></li> 
<li><img src="http://1.bp.blogspot.com/-MpDQSvxf9s0/VWXlWnzC3NI/AAAAAAAAF-c/HlZS-DDyosg/w1200-h630-p-k-no-nu/fundo-vermelho%2B%25281%2529.jpg" alt=""></li> 
<li><img src="http://images.all-free-download.com/images/graphiclarge/free_swirly_blue_stars_vector_background_148475.jpg" alt=""></li> 
<li><img src="http://1.bp.blogspot.com/-MpDQSvxf9s0/VWXlWnzC3NI/AAAAAAAAF-c/HlZS-DDyosg/w1200-h630-p-k-no-nu/fundo-vermelho%2B%25281%2529.jpg" alt=""></li> 
<li><img src="http://images.all-free-download.com/images/graphiclarge/free_swirly_blue_stars_vector_background_148475.jpg" alt=""></li> 
<li><img src="http://1.bp.blogspot.com/-MpDQSvxf9s0/VWXlWnzC3NI/AAAAAAAAF-c/HlZS-DDyosg/w1200-h630-p-k-no-nu/fundo-vermelho%2B%25281%2529.jpg" alt=""></li> 
<li><img src="http://images.all-free-download.com/images/graphiclarge/free_swirly_blue_stars_vector_background_148475.jpg" alt=""></li> 
</ul> 

</div> 

CSS:

div{ 
    width:835px; 
} 
li{ 
    display:inline-block; 
    list-style:none; 
    width:262px; 
} 
img{ 
    width:100%; 
} 
+0

嘗試https://masonry.desandro.com/ –

回答

2

這是比較容易實現與Masonry

砌體不需要jQuery,它可以作爲一個獨立的庫使用。最好的部分是你可以用HTML初始化它。 minifeid庫的大小約爲24Kb。如果使用Gzip compression

這個步驟很簡單。

  1. 負載使用

    <script src="https://unpkg.com/[email protected]/dist/masonry.pkgd.min.js></script> 
    
  2. 找到網格包裝div,並用它來初始化或用磚石在我 例如圖書館。

    <div class="grid" data-masonry='{ "itemSelector": ".item"}'> 
    
  3. 添加media queries根據您 個人喜好做出響應的物品。

工作響應例如:

.grid { 
 
    width: 100%; 
 
    margin: 1em auto; 
 
    text-align: center; 
 
} 
 

 
.item img { 
 
    display: block; 
 
    width: 100%; 
 
} 
 

 
.item { 
 
    margin: 0 auto; 
 
    box-sizing: border-box; 
 
    padding: 3px; 
 
} 
 

 

 
/* Media Queries */ 
 

 
@media (min-width: 0px) { 
 
    .item { 
 
    width: 99%; 
 
    } 
 
} 
 

 
@media (min-width: 500px) { 
 
    .item { 
 
    width: 49% 
 
    } 
 
} 
 

 
@media (min-width: 1000px) { 
 
    .item { 
 
    width: 32.33%; 
 
    } 
 
} 
 

 
@media (min-width: 2160px) { 
 
    .item { 
 
    width: 24% 
 
    } 
 
}
<script src="https://unpkg.com/[email protected]/dist/masonry.pkgd.min.js"></script> 
 

 

 
<div class="grid" data-masonry='{ "itemSelector": ".item"}'> 
 

 
    <div class="item"> 
 
    <img src="https://cdn.pixabay.com/photo/2016/04/15/04/02/water-1330252_960_720.jpg"> 
 
    </div> 
 

 
    <div class="item"> 
 
    <img src="https://image.freepik.com/free-vector/blue-background_1344-24.jpg"> 
 
    </div> 
 

 
    <div class="item"> 
 
    <img src="http://1.bp.blogspot.com/-MpDQSvxf9s0/VWXlWnzC3NI/AAAAAAAAF-c/HlZS-DDyosg/w1200-h630-p-k-no-nu/fundo-vermelho%2B%25281%2529.jpg"> 
 
    </div> 
 

 
    <div class="item"> 
 
    <img src="http://images.all-free-download.com/images/graphiclarge/free_swirly_blue_stars_vector_background_148475.jpg"> 
 
    </div> 
 

 
    <div class="item"> 
 
    <img src="http://1.bp.blogspot.com/-MpDQSvxf9s0/VWXlWnzC3NI/AAAAAAAAF-c/HlZS-DDyosg/w1200-h630-p-k-no-nu/fundo-vermelho%2B%25281%2529.jpg"> 
 
    </div> 
 

 
    <div class="item"> 
 
    <img src="http://images.all-free-download.com/images/graphiclarge/free_swirly_blue_stars_vector_background_148475.jpg"> 
 
    </div> 
 

 
    <div class="item"> 
 
    <img src="http://1.bp.blogspot.com/-MpDQSvxf9s0/VWXlWnzC3NI/AAAAAAAAF-c/HlZS-DDyosg/w1200-h630-p-k-no-nu/fundo-vermelho%2B%25281%2529.jpg"> 
 
    </div> 
 

 
    <div class="item"> 
 
    <img src="http://images.all-free-download.com/images/graphiclarge/free_swirly_blue_stars_vector_background_148475.jpg"> 
 
    </div> 
 

 
</div>

這可以通過使用imagesLoaded庫,以確保進一步提高砌築不觸發,直到所有的照片都完全加載。這有助於防止破碎的佈局。但是,我不會這麼做,因爲這超出了這個問題的範圍。

0

這個怎麼樣?

div { 
 
    width: 835px; 
 
} 
 

 
.col { 
 
    width: 30%; 
 
    display: inline-block; 
 
    vertical-align: top; 
 
} 
 

 
li { 
 
    display: inline-block; 
 
    list-style: none; 
 
} 
 

 

 
img { 
 
    width: 100%; 
 
}
<div> 
 
    <div class="col"> 
 
    <li><img src="https://cdn.pixabay.com/photo/2016/04/15/04/02/water-1330252_960_720.jpg" alt=""></li> 
 
    <li><img src="https://image.freepik.com/free-vector/blue-background_1344-24.jpg" alt=""></li> 
 
    <li><img src="http://1.bp.blogspot.com/-MpDQSvxf9s0/VWXlWnzC3NI/AAAAAAAAF-c/HlZS-DDyosg/w1200-h630-p-k-no-nu/fundo-vermelho%2B%25281%2529.jpg" alt=""> 
 
    </li> 
 
    </div> 
 
    <div class="col"> 
 
    <li><img src="http://images.all-free-download.com/images/graphiclarge/free_swirly_blue_stars_vector_background_148475.jpg" alt=""></li> 
 
    <li><img src="http://1.bp.blogspot.com/-MpDQSvxf9s0/VWXlWnzC3NI/AAAAAAAAF-c/HlZS-DDyosg/w1200-h630-p-k-no-nu/fundo-vermelho%2B%25281%2529.jpg" alt=""></li> 
 
    <li><img src="http://images.all-free-download.com/images/graphiclarge/free_swirly_blue_stars_vector_background_148475.jpg" alt=""></li> 
 
    </div> 
 

 
    <div class="col"> 
 
    <li><img src="http://1.bp.blogspot.com/-MpDQSvxf9s0/VWXlWnzC3NI/AAAAAAAAF-c/HlZS-DDyosg/w1200-h630-p-k-no-nu/fundo-vermelho%2B%25281%2529.jpg" alt=""></li> 
 
    <li><img src="http://images.all-free-download.com/images/graphiclarge/free_swirly_blue_stars_vector_background_148475.jpg" alt=""></li> 
 
    </div> 
 

 
</div>