2015-11-09 18 views
1

我正在嘗試使用表格創建水平圖像滑動條。我想要顯示5張圖像,無論頁面寬度(它的大小是否正確),但是,滾動不起作用。嘗試使用表格獲得水平滾動

我懷疑我在表中使用了錯誤的css。

如果有更好的解決方法,我不需要使用表格。

這裏是小提琴:https://jsfiddle.net/vsadm9qz/

HTML

<div class="galleryWrap"> 
    <table border="0"> 
     <tbody> 
      <tr> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="1" src="http://placehold.it/300x300?text=IMG+1" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="2" src="http://placehold.it/300x300?text=IMG+2" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="3" src="http://placehold.it/300x300?text=IMG+3" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="4" src="http://placehold.it/300x300?text=IMG+4" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="5" src="http://placehold.it/300x300?text=IMG+5" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="6" src="http://placehold.it/300x300?text=IMG+6" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="7" src="http://placehold.it/300x300?text=IMG+7" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="8" src="http://placehold.it/300x300?text=IMG+8" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="9" src="http://placehold.it/300x300?text=IMG+9" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="10" src="http://placehold.it/300x300?text=IMG+10" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="11" src="http://placehold.it/300x300?text=IMG+11" style="max-height: 390px;" /> 
        </div> 
       </td> 
       <td> 
        <div class="horizontalGalleryImageHolder"> 
         <img id="12" src="http://placehold.it/300x300?text=IMG+12" style="max-height: 390px;" /> 
        </div> 
       </td> 
      </tr> 
     </tbody> 
    </table> 
</div> 

CSS

body { 
    margin: 0; 
} 
table { 
    border-collapse: collapse; 
} 
td { 
    width: 20%; 
    padding: 0px; 
} 
img { 
    width: 100% 
} 
.galleryWrap { 
    width: 100%; 
    overflow-x: scroll; 
} 
+0

表是不是真的適合這種。你可以使用其他的東西嗎? – Harangue

+0

是的,我嘗試使用'inline-block',但是也失敗了,桌子只是我最接近的。但無論如何,只要我仍然只能顯示5張圖像,而且它們之間沒有空格,它可以做得很好。 –

回答

1

添加這些樣式:

td { 
    display: inline-block; 
} 

table { 
    white-space: nowrap; //prevent cells from wrapping 
} 

Fiddle 1

您還可以通過擺脫所有的表的HTML簡化代碼,一個d。使用這些樣式:

.galleryWrap { 
    white-space: nowrap; 
    width: 100%; 
    overflow-x: scroll; 
} 

.horizontalGalleryImageHolder { 
    width: 20%; 
    display: inline-block; 
} 

Fiddle 2

+0

我可以發誓我已經試過了!不過,這正是我期待的確切效果,謝謝! –

+1

**小提琴2 **也超級乾淨,現在我有選擇!再次感謝。 –

0

在這裏你去!

https://jsfiddle.net/vsadm9qz/2/

爲滑塊標準過程是內聯塊中的圖像的容器和在兩個div包裹它們。第一個有overflow-x: scroll,第二個寬度足以容納所有的圖像。

像這樣:

<div class="scroll"> 
    <div class="galleryWrap"> 
     <div class="image"><img></div> 

.image{ 
    display: inline-block; 
    width: 8% // 100/number of images 
} 

img { 
    width: 100% 
} 

.galleryWrap { 
    width: 1200%; // 100 * number of images 
} 

.scroll { 
    overflow-x: scroll; 
} 
+0

因此,看起來如果我們能夠將我的和你的相結合,我會得到我期待的效果,你的小提琴滑動,但圖像沒有大小的頁面,其中 - 因爲我有正確的圖像大小,但沒有滾動。我不明白爲什麼它不能正常工作:/ –

+0

它是根據頁面大小而定的。由於您的圖像數量(12)不會均勻分配到100(100/12 = 8.3333333),我將向下舍入爲8%。您可以使用不同數量的圖像或使用不同的近似值(8.25%)來更近一些。 – Harangue

+0

我把你的小提琴和改變了一些東西,寬度需要約** 1.7%**我不知道爲什麼,或者它是相對的。我現在唯一的問題是滾動距圖像有多遠,因爲寬度太長,如果我縮短包裝寬度,會使圖像變小。看到小提琴https://jsfiddle.net/vsadm9qz/5/ –