2015-02-10 155 views
1

我需要從左到右堆疊框(自下而上),並儘可能簡單地進行操作(無論是純粹的CSS還是使用JS,無論哪個更簡單)。請參閱下面的說明。從左到右從左到右堆疊框用CSS/JS

如果只有一個非全行

┌─────────────────────────┐ 
│       │ 
│       │ 
│  1 2 3 4  │ 
│       │ 
│       │ 
└─────────────────────────┘ 

一個完全填充行:

┌─────────────────────────┐ 
│       │ 
│       │ 
│ 1 2 3 4 5 6 7 │ 
│       │ 
│       │ 
└─────────────────────────┘ 

多行:

┌─────────────────────────┐ 
│       │ 
│       │ 
│ 8 9 10    │ 
│ 1 2 3 4 5 6 7 │ 
│       │ 
└─────────────────────────┘ 

許多行:

┌─────────────────────────┐ 
│       │ 
│       │ 
│ 15 16 17    │ 
│ 8 9 10 11 12 13 14 │ 
│ 1 2 3 4 5 6 7 │ 
└─────────────────────────┘ 

很難,但不顯示圖來描述。實現這個最簡單的方法是什麼?什麼是HTML5的方式?

編輯:

添加了此琴展示我的進步迄今:http://jsfiddle.net/12Lk7h45/

我需要的底部排完全充滿,而頂行應部分填充。另外,如何設置一個規則,當元素少於整行時,它應該都是水平居中的?

如果您不想點擊進入小提琴,這裏是我寫下來:

HTML:

<div class="wrapper"> 
    <div class="box"><div class="label">1</div></div> 
    <div class="box"><div class="label">2</div></div> 
    <div class="box"><div class="label">3</div></div> 
    <div class="box"><div class="label">4</div></div> 
    <div class="box"><div class="label">5</div></div> 
    <div class="box"><div class="label">6</div></div> 
    <div class="box"><div class="label">7</div></div> 
    <div class="box"><div class="label">8</div></div> 
    <div class="box"><div class="label">9</div></div> 
    <div class="box"><div class="label">10</div></div> 
    <div class="box"><div class="label">11</div></div> 
    <div class="box"><div class="label">12</div></div> 
</div> 

CSS:

.wrapper { 
    width: 100% 
} 

.box { 
    width: 50px; 
    height: 50px; 
    background-color: red; 
    text-align: center; 
    margin: 5px; 
    float: left; 
} 

.label { 
    position: relative; 
    top: 50%; 
    transform: translateY(-50%); 
} 

.box:nth-child(5n+1) { 
    background-color: green; 
    clear: left; 
} 
+1

你嘗試過什麼?也許做一個小提琴,我們可以幫助你更好。 – jungy 2015-02-10 02:59:29

+1

我同意上面的評論。如果你希望我們幫助你,那麼你需要給我們一些工作,'源'不是圖像。這也是一個不錯的主意,可以顯示你的嘗試。 – NewToJS 2015-02-10 03:01:59

+0

@jungy我不知道從哪裏開始,所以很遺憾我沒有得到比這更遠的地方:http://jsfiddle.net/12Lk7h45/ – adrianmc 2015-02-10 03:09:05

回答

2

最簡單的方法可能是使用Flexbox的。

在這裏,我在Code Pen上做了。您可以使用填充工具,如果你有兼容性問題與柔性盒

http://codepen.io/cjthedizzy/pen/wBpadE

.flex-container { 
    padding: 0; 
    margin: 0; 
list-style: none; 
flex-flow: row-reverse wrap-reverse; //THIS CHANGES THE FLOW TO REVERSE 
-ms-box-orient: horizontal; 
display: -webkit-box; 
display: -moz-box; 
display: -ms-flexbox; 
display: -moz-flex; 
display: -webkit-flex; 
display: flex; 
width:500px; 
background:#000; 
} 

填充工具 http://flexiejs.com/

+0

我比其他人更喜歡這個答案,它看起來更優雅,因爲它不依賴於旋轉的東西。 – adrianmc 2015-02-10 04:11:52

+1

我也檢查了caniuse和唯一的瀏覽器flexbox不工作是歌劇迷,當然舊版本的IE – ChrisJ 2015-02-10 04:17:13

3

你可以使用我的代碼,仍然不好,但工作不壞 my demo(只使用CSS)

<div style="" id="content"> 
    <div style="height: 50%; width: 100%; position: absolute; bottom: 0px;">    
     <ul> 
      <li>1</li> 
      <li>2</li> 
      <li>3</li> 
      <li>4</li> 
      <li>5</li> 
      <li>6</li> 
      <li>7</li> 
      <li>8</li> 
      <li>9</li> 
      <li>10</li> 
      <li>11</li> 
      <li>12</li> 
      <li>13</li> 
      <li>14</li> 
      <li>15</li> 
      <li>16</li> 
      <li>17</li> 
      <li>18</li> 
      <li>19</li> 
      <li>20</li> 
     </ul> 
     </div> 
</div> 

CSS:

#content{ 
width: 200px; 
background: none repeat scroll 0% 0% rgb(237, 237, 237); 
height: 200px; 
position: relative; 
} 
#content li { 
    display: inline-block; 
    float: right; 
    margin: 0 5px; 
    transform: rotate(-180deg); 
} 
ul{ 
margin: 0px; 
list-style: outside none none; 
width: 100%; 
transform: rotate(180deg); 
padding: 0px 20px; 
float: left; 
bottom: 0px; 
box-sizing: border-box; 
} 
+0

這真的很有趣,所以你只需要將它從右到左排列,旋轉整個容器,然後旋轉標籤? – adrianmc 2015-02-10 03:53:16

+0

是的。旋轉(180)爲ul並旋轉(-180)爲li。 – 2015-02-10 04:06:41

相關問題