简体   繁体   English

简单的下拉菜单按钮

[英]Simple drop down menu button

I'm trying to combine the drop down button found here Dropdown button with the menu icon found here Menu icon .我正在尝试将此处找到的下拉按钮与此处找到的菜单图标菜单图标 结合使用下拉按钮。 The animation would be nice to keep but is not the priority.动画会很好保留,但不是优先事项。 In my attempt I simply replaced the "Dropdown" text with the divs from the menu icon example, ie from this:在我的尝试中,我只是用菜单图标示例中的 div 替换了“下拉”文本,即从这个:

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

to this:对此:

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

and of course added the css classes.当然还添加了 css 类。 Now, the problem is that the bars inside the button are no longer clickable (At least not in Chrome. Firefox seems to work fine).现在,问题是按钮内的栏不再可点击(至少在 Chrome 中不是。Firefox 似乎工作正常)。 I've tried to add onclick="myFunction()" to the divs aswell but to no avail.我也尝试将onclick="myFunction()"到 div 中,但无济于事。

Any tips?有小费吗?

Here is the working fiddle for the same: https://jsfiddle.net/w5qftsgm/这是相同的工作小提琴: https : //jsfiddle.net/w5qftsgm/

I used a single function for both dropdown and bars which is written below:我对下拉菜单和栏使用了一个函数,如下所示:

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

Please feel free to ask if you have any queries如果您有任何疑问,请随时询问

Content inside the button tag is for visual purposes only.按钮标签内的内容仅用于视觉目的。

All interactivity inside is strictly limited to the button itself.内部的所有交互都严格限于按钮本身。

Therefore you should have the bars on the same level as the button.因此,您应该将条形与按钮置于同一级别。

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

if you need dropdown, i recommend change your code, for ex如果您需要下拉菜单,我建议更改您的代码,例如

 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>

Don't use a button with div's in it.不要使用带有 div 的按钮。 Try this solution from W3schools:试试 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>

Here you have a live demo: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button这里有一个现场演示: https : //www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button

use an anchor tag and style it you can use an anchor tag without a href and make sure you are using a high z-index使用锚标记并设置样式 您可以使用没有 href 的锚标记,并确保您使用的是高 z-index

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

I created fiddle for you.我为你创造了小提琴。 First you have to style your bars.首先,你必须设计你的酒吧。 Then you need to create function that will add or remove show class in order to display your content.然后您需要创建函数来添加或删除显示类以显示您的内容。 Something like that:类似的东西:

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')
  }
})

Here you have styles:这里有样式:

.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/ https://jsfiddle.net/ajp02uby/

this is your HTML这是你的 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>

And your script还有你的剧本

/* 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');
      }
    }
  }
}

And CSS goes here 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);
}

Please consider changing the div inside the button to some inline elements like span.请考虑将按钮内的 div 更改为一些内联元素,例如 span。 In HTML try to avoid using of block elements inside inline elements.在 HTML 中尽量避免在内联元素中使用块元素。

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

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