javascript - 将下拉菜单置于触发它的按钮下方

标签 javascript html css drop-down-menu

我正在尝试创建一个下拉菜单,以便在一个简单的网站上更轻松地导航。我从 w3schools 借用了大量代码.免责声明是,我真的很不擅长样式和 js。

问题是当我添加一个额外的按钮时,菜单显示在第一个按钮下。

function myFunction(a) {

  document.getElementById(a).classList.toggle("show");

}
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');
      }
    }
  }
}
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}


/* Dropdown button on hover & focus */

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


/* 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: #f1f1f1;
  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: #ddd
}


/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */

.show {
  display: block;
}
<div class="dropdown">
  <button onclick="myFunction('myDropdown0')" class="dropbtn">Menu1</button>
  <div id="myDropdown0" class="dropdown-content">
    <a href="{% url 'App1:index' %}">choice1</a>
  </div>
  <button onclick="myFunction('myDropdown1')" class="dropbtn">Menu2</button>
  <div id="myDropdown1" class="dropdown-content">
    <a href="{% url 'App2:index' %}">Choice1</a>
    <a href="{% url 'App2:page2' %}">Choice2</a>
  </div>
</div>

感觉应该有一种简单的方法来编辑它以考虑按下按钮的位置,但我不确定如何。

最佳答案

您应该更新您的 window.onclick 函数和 HTML div 标签。一种解决方案是先关闭所有菜单,然后再打开菜单。

请尝试:

function myFunction(a) {
		closeAllMenu();
        document.getElementById(a).classList.toggle("show");
}

function closeAllMenu(){//before show menu, close all menu first
        var dropdowns = document.getElementsByClassName("dropdown-content");
        var i;
        for (i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
                openDropdown.classList.remove('show'); 
        }
}

window.onclick = function(event) { // click outside the menu, close menu
    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');
            }
        }
    }
}
.dropbtn {
    background-color: #3498DB;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
    background-color: #2980B9;
}

/* 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: #f1f1f1;
    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: #ddd}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
	<div class="dropdown">
		<button onclick="myFunction('myDropdown0')" class="dropbtn">Menu1</button>
		<div id="myDropdown0" class="dropdown-content">
			<a href="{% url 'App1:index' %}">choice1</a>
		</div>
	</div>
	<div class="dropdown">
		<button onclick="myFunction('myDropdown1')" class="dropbtn">Menu2</button>
		<div id="myDropdown1" class="dropdown-content">
			<a href="{% url 'App2:index' %}">Choice1</a>
			<a href="{% url 'App2:page2' %}">Choice2</a>
		</div>
	</div>

关于javascript - 将下拉菜单置于触发它的按钮下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50306788/

相关文章:

javascript - 如何使弹出div可拖动

html - Chrome 后台修复问题

javascript - 样式化提交按钮——Rails 应用程序

html - CSS3 淡入淡出背景图像动画 - Firefox

javascript - 如何让 navbar-collapse 与动画汉堡一起使用?

javascript - 为什么我的 JavaScript 搜索功能重定向到 "http://localhost:8090/?"?

html - 如何在打印预览中将元素与页面底部对齐

javascript - 使用 gulp 更新 serviceworker 配置

javascript - 使用 HTML、CSS 和 JavaScript 构建图像中的图像

javascript - 展望。正则表达式