2012-12-21 128 views
1

首先,here's the dropdown/collapsible menu我已經構建。相對位置菜單項在下拉/摺疊菜單中?

至於什麼是「菜單」和「菜單項目」是在搗鼓出the preview - 頻道搜索關於我們的菜單,而那些下拉/當你點擊菜單時滑出是菜單項。

截圖:

screenshot

我使用的菜單position: absolute;上的菜單項(.collapse),和position: relative;

的相關代碼more in the fiddle):

/* Menu: <li class="float-left top-menu">... */ 
.top-menu { 
    position: relative; 
} 

/* Menu-Item: <div id="channels-menu-item-container" class="collapse">... */ 
.collapse { 
    position: absolute; 
    width: 570px; 
    z-index: 1000; 
    padding: 0; 
    margin: 0; 
    color: #222; 
    background-color: #fff; 
    -webkit-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.2), 0 10px 20px rgba(0, 0, 0, 0.12); 
    -moz-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.2), 0 10px 20px rgba(0, 0, 0, 0.12); 
    box-shadow: 0 5px 5px rgba(0, 0, 0, 0.2), 0 10px 20px rgba(0, 0, 0, 0.12); 
    overflow: hidden; 
} 

是有可能實現的 「菜單項」 使用position: relative;相同的功能(.collapse)呢?


注:在你提交的答案與提琴,請確保在兩個小提琴菜單(你和我)都通過單擊每個菜單,比較(所以功能相同以確認沒有菜單項由於定位改變而錯位)。

當然,您可以添加額外的HTML和CSS位。


是的,我試了幾個小時徒勞無功,想看看它是否可能。

+0

如果我可能會問 - 「absolute」有什麼問題?在'relative'中絕對位置的工作很好(例如,'right:0;'在右對齊菜單中對你很有幫助) - 什麼不適合你?另外,我在其他框下面看到了「版本」框,而不是在右側(在Firefox 17上) – Kobi

+0

@Kobi是的,確切地說。你看到的是因爲我在菜單項('.collapse')上有一個指定的寬度,但我不需要它。如果我刪除寬度,菜單項不再是水平的,即垂直摺疊。但是,如果我可以讓菜單使用'relative',我可以使用'inline-block'而不是'float'作爲菜單項中的列。明白了嗎? –

+0

我這麼認爲。那麼,真正的問題是如何獲得自動寬度? – Kobi

回答

1

怎麼是這樣的:

  • .collapse刪除寬度。
  • 從列中移除浮動,更改爲inline-block
  • 更改white-spacenowrap

HTML:

<div class="in collapse" id="channels-menu-item-container" style="height: auto;"> 
    <div id="channels-menu-item-wrapper"> 
     <ul class="channel-column" id="nav-channels"> 
      <!-- ... --> 
     </ul> 
     <ul class="channel-column" id="nav-topics"> 
      <!-- ... --> 
     </ul> 
     <ul class="channel-column" id="nav-editions"> 
      <!-- ... --> 
     </ul> 
     <div class="aahans"></div> 
     <div class="clearfix"></div> 
    </div> 
</div> 

的CSS:

.collapse { 
    position: absolute; 
    min-width:200px; /*for the seach box*/ 
    /* ... */ 
} 

#channels-menu-item-wrapper { 
    border-bottom: 4px solid #259; 
    white-space:nowrap; 
} 

.channel-column {display:inline-block; vertical-align:top;} 

您可能還需要重置每個.channel-columnwhite-space值。

結果:http://jsfiddle.net/kobi/fxSYT/1/embedded/result/

+1

我很快在我的開發網站上覆制了這個,它似乎工作得很好,因此將其標記爲答案。由於已經過了凌晨2點,因此無法好好觀察。 **編輯:**您建議將'white-space'改爲'no-break',但您答案中的CSS使用'nowrap'。那是一個錯誤嗎?另外,你是什麼意思,「你可能還想重置每個'.channel-column'中的'white-space'值。你的意思是,在'.channel-column'上使用'white-space:normal'是個好主意嗎? (** PS:**非常感謝!) –

+1

@its_me - 是的,這就是我的意思,以防萬一(例如,您可能在列中有文本塊)。樂於幫助! (另外,請記住[明確問題](http://meta.stackexchange.com/q/66377/7586)。晚安! – Kobi

+0

編輯。我認爲我編造了'no-break'。謝謝': )' – Kobi