简单的下拉菜单按钮

时间:2018-12-28 10:55:29

标签: javascript html css

我正在尝试合并此处找到的下拉按钮 Dropdown button,并在此处Menu icon找到菜单图标。动画会很好保留,但不是优先级。在我的尝试中,我只是将菜单图标示例中的div替换为“ Dropdown”文本,即:

<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中,但无济于事。

有什么提示吗?

7 个答案:

答案 0 :(得分:1)

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

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

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

请随时询问您是否有疑问

答案 1 :(得分:0)

button标记内的内容仅用于视觉目的。

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

因此,条形图应与按钮处于同一水平。

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

答案 2 :(得分:0)

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

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>

答案 3 :(得分:0)

请勿在其中使用带有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

答案 4 :(得分:0)

使用定位标记并设置其样式,您可以使用不带有href和 确保您使用了较高的Z-index

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

答案 5 :(得分:0)

我为您制作了小提琴。首先,您必须设置自己的酒吧风格。然后,您需要创建将添加或删除show类的功能以显示您的内容。像这样:

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/

答案 6 :(得分:0)

这是您的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中,请尝试避免在内联元素内使用块元素。

相关问题