繁体   English   中英

简单的下拉菜单按钮

[英]Simple drop down menu button

我正在尝试将此处找到的下拉按钮与此处找到的菜单图标菜单图标 结合使用下拉按钮。 动画会很好保留,但不是优先事项。 在我的尝试中,我只是用菜单图标示例中的 div 替换了“下拉”文本,即从这个:

<button onclick="myFunction()" class="dropbtn">Dropdown</button>

对此:

<button onclick="myFunction()" class="dropbtn">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
</button>

当然还添加了 css 类。 现在,问题是按钮内的栏不再可点击(至少在 Chrome 中不是。Firefox 似乎工作正常)。 我也尝试将onclick="myFunction()"到 div 中,但无济于事。

有小费吗?

这是相同的工作小提琴: https : //jsfiddle.net/w5qftsgm/

我对下拉菜单和栏使用了一个函数,如下所示:

function myFunction(x) {
  x.classList.toggle("change");
  document.getElementById("myDropdown").classList.toggle("show");
}

如果您有任何疑问,请随时询问

按钮标签内的内容仅用于视觉目的。

内部的所有交互都严格限于按钮本身。

因此,您应该将条形与按钮置于同一级别。

<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div class="dropbtn">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
</div>

如果您需要下拉菜单,我建议更改您的代码,例如

 function myFunc(e){ console.log(e.target) } document.getElementById("button").addEventListener("click",myFunc)
 <div id="button" class="dropbtn"> <button class="bar1">1</button> <button class="bar2">2</button> <button class="bar3">3</button> </div>

不要使用带有 div 的按钮。 试试 W3schools 的这个解决方案:

<style>
/* Style The Dropdown Button */
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
  display: block;
}

/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
</style>

<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

这里有一个现场演示: https : //www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button

使用锚标记并设置样式 您可以使用没有 href 的锚标记,并确保您使用的是高 z-index

<a style='z-index:999;'>bars go here</a>

我为你创造了小提琴。 首先,你必须设计你的酒吧。 然后您需要创建函数来添加或删除显示类以显示您的内容。 类似的东西:

const btn = document.querySelector('.dropbtn');
const content = document.querySelector('.content');
btn.addEventListener('click', function() {
    if(!content.classList.contains('show')) {
    content.classList.add('show')
  }
  else {
    content.classList.remove('show')
  }
})

这里有样式:

.bar {
 width:20px;
 height:3px;
 background:black;
 margin: 2px;
}
.content {
  width:100px;
  height:200px;
  background:gray;
  display:none;
}
.content.show {
  display: block;
}

https://jsfiddle.net/ajp02uby/

这是你的 HTML

    <div class="dropdown">
   <button onclick="myFunction(this)" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
    <div id="myDropdown" class="dropdown-content">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div>
</div>

还有你的剧本

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction(x) {
x.classList.toggle("change");
  document.getElementById("myDropdown").classList.toggle("show");
}


// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

CSS 在这里

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

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

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

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  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 a:hover {background-color: #ddd;}

.show {display: block;}
.container {
  display: inline-block;
  cursor: pointer;
}

.bar1, .bar2, .bar3 {
  width: 35px;
  height: 5px;
  background-color: #333;
  margin: 6px 0;
  transition: 0.4s;
}

.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 6px);
  transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {opacity: 0;}

.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -8px);
  transform: rotate(45deg) translate(-8px, -8px);
}

请考虑将按钮内的 div 更改为一些内联元素,例如 span。 在 HTML 中尽量避免在内联元素中使用块元素。

暂无
暂无

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

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