如何指定子菜单div的位置并防止菜单div更改

时间:2016-08-02 10:33:36

标签: html css drop-down-menu menu submenu

我从here开始关注下拉菜单的教程。

但是当我尝试实现子菜单时,我陷入困境。到目前为止,我已设法制作此代码。 HTML:

<div class="dropdown" style="float:left;">
  <button class="dropbtn">Left</button>
  <div class="dropdown-content" style="left:0;">
    <div class="dropdown-list draggable" href="#">Link 1
        <div class="dropdown-sub-list">
          <div class="draggable" href="#">Link 4</div>
          <div class="draggable" href="#">Link 5</div>
          <div class="draggable" href="#">Link 6</div>
          <div class="draggable" href="#">Link 7</div>
        </div>
    </div>
    <div class="dropdown-list draggable" href="#">Link 2
    <div class="dropdown-sub-list">
          <div class="draggable" href="#">Link 4b</div>
          <div class="draggable" href="#">Link 5b</div>
          <div class="draggable" href="#">Link 6b</div>
          <div class="draggable" href="#">Link 7b</div>
        </div>
    </div>
    <div class="dropdown-list draggable" href="#">Link 3</div>
  </div>
</div>

CSS:

.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-sub-list {
    display: none;
    position:relative;
    float:left;
    right: 0;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content .draggable {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown-content .draggable:hover{background-color: #f1f1f1}

.dropdown-list:hover .dropdown-sub-list{
    display : block;
}

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}

这是jsfiddle: https://jsfiddle.net/yusrilmaulidanraji/9sdck1z1/3/

我的问题是:

  • 如何将子菜单移动到菜单右侧的确切位置?
  • 如何防止因子菜单外观而调整菜单大小?

提前谢谢。

5 个答案:

答案 0 :(得分:1)

Updated Fiddle

  1. position: relative中添加.dropdown-content .draggable(以便下拉菜单可以定位到右侧)

  2. position: absolute上使用.dropdown-sub-list。同时添加left: 100%(向下移动到右侧); top: 0(向下移动到顶部)
  3. &#13;
    &#13;
    .dropbtn {
      background-color: #4CAF50;
      color: white;
      padding: 16px;
      font-size: 16px;
      border: none;
      cursor: pointer;
    }
    .dropdown {
      position: relative;
      display: inline-block;
    }
    .dropdown-content {
      display: none;
      position: absolute;
      right: 0;
      background-color: #f9f9f9;
      min-width: 160px;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    }
    .dropdown-sub-list {
      display: none;
      position: absolute;
      top: 0;
      left: 100%;
      background-color: #f9f9f9;
      min-width: 160px;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    }
    .dropdown-content .draggable {
      color: black;
      padding: 12px 16px;
      text-decoration: none;
      display: block;
      position: relative;
    }
    .dropdown-content .draggable:hover {
      background-color: #f1f1f1
    }
    .dropdown-list:hover .dropdown-sub-list {
      display: block;
    }
    .dropdown:hover .dropdown-content {
      display: block;
    }
    .dropdown:hover .dropbtn {
      background-color: #3e8e41;
    }
    &#13;
    <div class="dropdown" style="float:left;">
      <button class="dropbtn">Left</button>
      <div class="dropdown-content" style="left:0;">
        <div class="dropdown-list draggable" href="#">Link 1
          <div class="dropdown-sub-list">
            <div class="draggable" href="#">Link 4</div>
            <div class="draggable" href="#">Link 5</div>
            <div class="draggable" href="#">Link 6</div>
            <div class="draggable" href="#">Link 7</div>
          </div>
        </div>
        <div class="dropdown-list draggable" href="#">Link 2
          <div class="dropdown-sub-list">
            <div class="draggable" href="#">Link 4b</div>
            <div class="draggable" href="#">Link 5b</div>
            <div class="draggable" href="#">Link 6b</div>
            <div class="draggable" href="#">Link 7b</div>
          </div>
        </div>
        <div class="dropdown-list draggable" href="#">Link 3</div>
      </div>
    </div>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

用这个替换你的css:

.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropdown {

    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-sub-list {
    display: none;

    float:right;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content .draggable {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown-content .draggable:hover{background-color: #f1f1f1}

.dropdown-list:hover .dropdown-sub-list{
    display : block;
}

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}

答案 2 :(得分:0)

.dropdown-content {
    display: none;
    position: absolute; "change here"
    right: 0;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-sub-list {
    display: none;
    top: 40px;
    position: absolute; "change here"
    float: left; "not sure you need this"
    right: -41px;
    background-color: #f9f9f9;
    min-width: 160px;
    z-index: 1; "change here"
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

答案 3 :(得分:0)

在您的CSS检查小提琴链接

中完成了一些更改

https://jsfiddle.net/9sdck1z1/11/

.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-sub-list {
    background-color: #f9f9f9;
    display: none;
    float: left;
    min-width: 160px;
    position: absolute;
    right: -160px;
    top: 0;

}

.dropdown-content .draggable {
   color: black;
    display: block;
    padding: 12px 16px;
    position: relative;
    text-decoration: none;
}

.dropdown-content .draggable:hover{background-color: #f1f1f1}

.dropdown-list.draggable:hover .dropdown-sub-list{

  display : block;
}


.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}

答案 4 :(得分:0)

寻找基于CSS的下拉菜单,它们更加简单......而且很多都是毫无意义的#39;的DIV。

我没有设计它,但这就是你需要它才能使它发挥作用。

(你不需要类名.sub和.sub-2,但它可以帮助你在设计样式时跟踪事物)

HTML

<nav>
<ul>
    <li><a href="#">Left</a>
        <ul class="sub">
          <li><a href="#">Link 1</a>
            <ul class="sub-2">
              <li><a href="#">link 1 - 1</a></li>
              <li><a href="#">link 1 - 2</a></li>
              <li><a href="#">link 1 - 3</a></li>
            </ul> 
          </li>
          <li>link 2</li>
          <li>link 3</li>
        </ul> 
            </li>
</ul> 
</nav>

CSS:

nav li{list-style-type:none; }
nav ul.sub, nav ul.sub-2{display:none; }
nav ul li:hover .sub, nav .sub li:hover .sub-2{display:block; }