使下拉式選單多層化
P粉107772015
P粉107772015 2024-01-10 18:02:08
0
2
437

我有一個下拉式選單,如下所示:

.dropdown
{
    display: inline-block;
    position: relative;
}
.dropdown button > a
{
    display: block;
    color: #000000;
    text-decoration: none;
}
.dropdown-content
{
    display: none;
    position: absolute;
    width: auto;
    overflow: auto;
    box-shadow: 0px 10px 10px 0px rgba(0,0,0,0.4);
}
.dropdown:hover .dropdown-content
{
    display: block;
}
.dropdown-content a
{
    display: block;
    color: #000000;
    background-color: #e9e9ed;
    padding: 5px;
    text-decoration: none;
}
.dropdown-content a:hover
{
    color: #FFFFFF;
    background-color: #0080bf;
}
<DIV class="dropdown"><BUTTON>Menu</BUTTON><DIV class="dropdown-content">
    <A href="/1.php">Option 1</A>
    <A href="/2.php">Option 2</A>
    <A href="/3.php">Option 3</A>
    <DIV class="dropdown"><BUTTON><A href="/4.php">Option 4</A></BUTTON><DIV class="dropdown-content">
        <A href="/4-1.php">Option 4-1</A>
        <A href="/4-2.php">Option 4-2</A>
        <A href="/4-3.php">Option 4-3</A>
    </DIV></DIV>
</DIV></DIV>
但第二級選單不起作用(按“運行程式碼片段”即可查看)。我需要它顯示在第一級的右側,並且每級的寬度必須是彈性的(根據內容自動增加和減少)。此外,根按鈕的寬度不得綁定到第一級的寬度。理想情況下,任何級別都應該有通用風格,但這並不重要,因為我沒有數十個關卡。有沒有不用重寫所有程式碼的解決方案?

P粉107772015
P粉107772015

全部回覆(2)
P粉235202573

function showNestedDropdown() {
          var firstDropdown = document.getElementById("firstDropdown");
          var nestedDropdownContainer = document.getElementById("nestedDropdownContainer");
      
          if (firstDropdown.value !== "") {
            nestedDropdownContainer.style.display = "block";
          } else {
            nestedDropdownContainer.style.display = "none";
          }
        }
    <select id="firstDropdown" onchange="showNestedDropdown()">
        <option value="">Select an option</option>
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
      </select>
      
      <div id="nestedDropdownContainer" style="display: none;">
        <select id="nestedDropdown">
          <option value="">Select a nested option</option>
          <option value="nested1">Nested Option 1</option>
          <option value="nested2">Nested Option 2</option>
        </select>
      </div>
P粉187160883

您可以執行以下操作:

.dropdown-content 移除 overflow: auto;,以便溢出的子層級可見。在 .dropdown:hover .dropdown-content 行新增 > 選擇器,以便直接子項目會在懸停時顯示。最後一件事是新增子層級樣式以將其顯示在右上角 .dropdown-content .dropdown-content { left: 100%;頂部:0; }

body {
  background-color: antiquewhite;
}
.dropdown
{
    display: inline-block;
    position: relative;
    width: 100%;
}
.dropdown-content
{
    display: none;
    position: absolute;
    width: auto;
    box-shadow: 0px 10px 10px 0px rgba(0,0,0,0.4);
    white-space: nowrap;
}
.dropdown:hover > .dropdown-content
{
    display: block;
}
.dropdown-content a
{
    display: block;
    color: #000000;
    background-color: #e9e9ed;
    padding: 5px;
    text-decoration: none;
}
.dropdown-content a:hover
{
    color: #FFFFFF;
    background-color: #0080bf;
}

/* To display second level on right*/
.dropdown-content .dropdown-content {
    left: 100%;
    top: 0;
}
<div class="dropdown">
    <div class="menu">Menu</div>
    <div class="dropdown-content">
      <a href="/1.php">Option 1 Longer</a>
      <div class="dropdown">
          <div class="menu">
            <a href="/4.php">Option</a>
          </div>
          <div class="dropdown-content">
            <a href="/4-1.php">Option 4-1</a>
            <a href="/4-2.php">Option 4-2</a>
            <a href="/4-3.php">Option 4-3</a>
        </div>
      </div>
    </div>
</div>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板