我只是使用 html 代码(没有 Bootstrap )制作我的网站。我想制作一个包含规范列表的下拉列表。问题是当我打开我的列表时它完成后 - 下面的内容不会在下面移动更多,所以它在列表上重叠..看看这张照片看看我有什么。
所以在这里,当我打开列表时,下面的内容会自动出现在下面(但这是 Bootstrap ,所以我不能只复制它,因为我只使用 html)
我的html代码是这样的:
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
和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;}
和javascript:
<script>
function myFunction() {
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');
}
}
}
}
</script>
最佳答案
示例:https://codepen.io/anon/pen/VNojBe
- 从
.dropdown-content
中移除position: absolute;
- 在
.dropdown-content
中更改为position: relative
关于javascript - 下拉菜单无法正常工作。 (html),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55954914/