简体   繁体   English

JS / CSS下拉列表中的 <ul> 导航栏

[英]JS/CSS dropdown in an <ul> navbar

I am trying to make a drop down menu under a button in a navbar, but I can't get it to work quite right. 我正在尝试在导航栏中的按钮下方创建一个下拉菜单,但无法使其正常运行。

As you can see in the fiddle, I have a working navbar with the "settings" button (hammer + wrench), that shows the "dark theme" option. 如您在小提琴中看到的,我有一个带有“设置”按钮(锤子+扳手)的导航栏,其中显示了“深色主题”选项。

Here's the problem: I can't think of a way to make the "dark theme" option to be under the "settings" button since its inside the navbar, and adding the floating button to the navbar would cause some ugly effects (eg border and background color would not fit anymore). 这是问题所在:我想不出一种方法,因为它在导航栏中位于“设置”按钮下方,因此无法将“黑暗主题”选项置于“设置”按钮之下,并且将导航按钮添加到导航栏中会产生一些难看的效果(例如边框)和背景颜色不再适合)。

Link to the fiddle im talking about. 链接到我正在谈论的小提琴 Heres the HTML for the navbar and the button in question below: 以下是导航栏和所关注按钮的HTML:

<ul class="navBar">
  <div class="divLiLeft">
    <li>
      <a>
        <button class="navButton">Button 1</button>
      </a>
    </li>
  </div>
  <div class="divLiLeft">
    <li>
      <a>
        <button class="navButton">Button 2</button>
      </a>
    </li>
  </div>
  <div class="divLiLeft">
    <li>
      <a>
        <button class="navButton">Button 3</button>
      </a>
    </li>
  </div>
  <div>
    <div class="divLiRight">
      <li>
        <button id="dropButton" class="dropButton"></button>
      </li>
    </div>
  </div>
</ul>
<div id="dropContent" class="dropContent">
  <button id="change" class="navButton"></button>
</div>

And here is the CSS in question: 这是有问题的CSS:

    .navBar,.navBarNew {
      list-style-type: none;
      overflow: hidden;
      position: fixed;
      top: 0;
      left: 0;
      padding: 0;
      margin: 0px;
      border-bottom: solid 1px #3E3D3D;
      width: 100%;
      background-color: #747474;
      transition-duration: 0.4s;
      min-width: 100px;
    }
    .divLiLeft {
      display: inline-block;
      float: left;
    }

.divLiRight {
  display: inline-block;
  float: right;
}

PS: you might have to expand the window to show the settings button. PS:您可能需要扩展窗口才能显示设置按钮。

You should add position:relative; 您应该添加position:relative; to your div .divLiRight and add right:0 to .dropContent,.dropContentShow 到您的div .divLiRight并将right:0添加到.dropContent,.dropContentShow

The .dropdown class use position:relative , which is needed when we want the dropdown content to be placed right below the dropdown button (using position:absolute ). .dropdown类使用position:relative ,当我们希望将下拉内容放置在下拉按钮的正下方(使用position:absolute )时,需要使用该类。

The .dropdown-content class holds the actual dropdown menu. .dropdown-content类保存实际的下拉菜单。 It is hidden by default, and will be displayed on hover (see below). 默认情况下它是隐藏的,并且将在悬停时显示(请参阅下文)。 Note the min-width is set to 160px . 请注意, min-width设置为160px Feel free to change this. 随时更改此设置。 Tip: If you want the width of the dropdown content to be as wide as the dropdown button, set the width to 100% (and overflow:auto to enable scroll on small screens). 提示:如果您希望下拉内容的宽度与下拉按钮的宽度一样,请将宽度设置为100%(然后使用overflow:auto启用在小屏幕上滚动)。

Instead of using a border, we have used the box-shadow property to make the dropdown menu look like a "card". 我们没有使用边框,而是使用box-shadow属性使下拉菜单看起来像“卡片”。 We also use z-index to place the dropdown in front of other elements. 我们还使用z-index将下拉列表放置在其他元素的前面。

The :hover selector is used to show the dropdown menu when the user moves the mouse over the dropdown button. 当用户将鼠标移到下拉按钮上时, :hover选择器用于显示下拉菜单。

Referenced from https://www.w3schools.com/howto/howto_css_dropdown.asp 引用自https://www.w3schools.com/howto/howto_css_dropdown.asp

 .body, .bodyNew { background-color: #EEEEEE; padding-top: 40px; transition-duration: 0.4s; width:100%; height:100%; overflow: hidden; } .bodyNew { background-color: #202225; } .navBar, .navBarNew { list-style-type: none; overflow: hidden; position: fixed; top: 0; left: 0; padding: 0; margin: 0px; border-bottom: solid 1px #3E3D3D; width: 100%; background-color: #747474; transition-duration: 0.4s; min-width: 500px; } .navBarNew { border-bottom: solid 1px #44474C; width: 100%; background-color: #101217; } .divLiLeft { display: inline-block; float: left; } .divLiRight { display: inline-block; float: right; } .navButton, .navButtonNew { padding: 5px; font-size: 25px; background-color: #747474; color: #CCCCCC; border-right: solid 1px #3E3D3D; border-top: none; border-left: none; border-bottom: none; transition-duration: 0.4s; cursor: pointer; } .navButton:hover { background-color: #CCCCCC; color: #747474; } .navButton::-moz-focus-inner { border: none; } .navButtonNew { background-color: #101217; color: #44474C; border-right: solid 1px #44474C; } .navButtonNew:hover { background-color: #63666E; color: #0C1018; } .navButtonNew::-moz-focus-inner { border: none; } .dropdown { top: -48px; position: relative; display: inline-block; right:9px; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 140px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown:hover .dropdown-content { display: block; } .dropdown:hover .dropbtn { background-color: #CCCCCC; color: #747474; } .dropbtn{ border-left:solid 1px #3E3D3D; } 
 <!DOCTYPE html> <html> <head> </head> <body class="body"> <ul class="navBar"> <div class="divLiLeft"> <li> <a> <button class="navButton">Button 1</button> </a> </li> </div> <div class="divLiLeft"> <li> <a> <button class="navButton">Button 2</button> </a> </li> </div> <div class="divLiLeft"> <li> <a> <button class="navButton">Button 3</button> </a> </li> </div> </ul> <div class="divLiRight"> <div class="dropdown"> <button class="dropbtn navButton">Dropdown</button> <div class="dropdown-content"> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> </div> </div> </div> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM