2011-09-08 51 views
1

我試圖製作一個「滑出」div,當它懸停在它上面時滑出。我想要發生的是我想讓左邊的div將右邊的div推開,但不是低於div,而是右邊。 有誰知道爲什麼會發生這種情況?爲什麼DIV被推到下面而不是在旁邊?

這裏是我的腳本:

.container { 
    width: 796px; 
    background-color: yellow; 
    overflow:hidden; 
} 
.left { 
    display:none; 
    float: left; 
    width: 256px; 
    background: green; 
} 
.right { 
    float: right; 
    width: 796px; 
    height: 100%; 
    background-color: red; 
} 

Here is a live demo 點擊 '顏色框',然後選擇 '顯示左格'。

謝謝大家! :))))

回答

1

假設您的.left.right.container之內,被推倒的原因是因爲沒有足夠的空間。

您已設置爲.container等孩子即.left.right需要加起來796px一個width:796px。目前他們加起來爲1052px,所以他們中的一個被推倒了,因爲他們不能並排放在一起。

編輯:使用內容器方法在我的評論中提到和@ Matteo的答案中提到你需要調整下面的CSS。

.inner_container{ 
    width: 1396px; /* changed from 1052px */ 
} 
.right { 
    background-color: red; 
    float: left; /* changed from right */ 
    height: 100%; 
    width: 796px; 
} 

它需要1396px是因爲它需要足夠大,當.left擴展到600px.right769px仍然可以適應它旁邊的原因。然後更改爲float:left是必要的,以便.left.right之間沒有間隙,並且在側向推動時它仍保持可見。

+0

我不想容器 '放大',但對右列進行排序的去外面下起的容器。你明白我的意思嗎?這就是爲什麼我試圖溢出:隱藏。 – pufAmuf

+0

@pufAmuf:您可以爲'.container'設置一個固定的高度,並使'.left'和'.right'具有相同的固定高度。這樣,當一個人被推到另一個之下時,它將位於'.container'邊緣的下方,因此當你有'overflow:hidden'設置時不會顯示。或者如果高度會變化,可以使用足夠寬的內部容器以容納'.left'和'.right',同樣溢出將被隱藏。 – tw16

+0

嗯,我不能得到這個工作出於某種原因 – pufAmuf

1

.container內部添加另一個div,其寬度足以容納.left.right

http://jsfiddle.net/DRnRQ/

CSS:

.container { 
    width: 796px; 
    height: 300px; 
    background-color: yellow; 
    overflow:hidden; 
} 
.left { 
    float: left; 
    width: 256px; 
    background: green; 
} 
.right { 
    float: right; 
    width: 796px; 
    height: 100%; 
    background-color: red; 
} 
.inner_container { 
    width: 1052px; 
} 

MARKUP:

<div class="container"> 
    <div class="inner_container"> 
     <div class="left">a</div> 
     <div class="right">b</div> 
    </div> 
</div> 
+0

嗯,這似乎並不奏效。感謝您的提示,雖然:) – pufAmuf

+0

這必須工作,除非你使用一些破碎的瀏覽器。 –

+0

讓我對公開版本進行更改:) – pufAmuf

相關問題