英文:
How to avoid the dropdown from disappearing when removing cursor from the button?
问题
当我悬停在按钮上时,我看到下拉菜单,但是当我导航到菜单中的任何项时,下拉菜单就会消失。我认为问题在于我只在按钮上添加了悬停效果,但我应该如何使它像正常的下拉菜单一样工作?
<div class="dropdown">
<button class="dropbtn">更多</button>
<ul class="dropdown-content">
<li><a href="#">主页</a></li>
<li><a href="#">关于</a></li>
<li><a href="#">联系</a></li>
</ul>
</div>
.dropdown {
display: inline-block;
padding: 5px;
}
.dropbtn {
padding: 5px;
margin-bottom: 0px;
width: 100%;
}
.dropdown-content {
display: none;
list-style: none;
background: #f1f1f1;
position: relative;
top: 0px;
left: 0px;
padding: 0px 0px 0px 0px;
margin: 0px;
text-align: center;
z-index: 1;
width: 100%;
}
.dropdown-content li {
margin-bottom: 0px;
padding: 3px;
}
.dropdown-content li a {
text-decoration: none;
color: black;
padding: 3px;
}
.dropdown-content li:hover,
.dropdown-content li a:hover {
text-decoration: none;
color: white;
background-color: #333;
}
.dropbtn:hover+.dropdown-content, .dropdown-content:hover {
display: block;
}
我希望在我悬停在下拉菜单的子元素上时,下拉菜单保持可见,就像其他下拉菜单一样工作。
英文:
When I hover over the button I see the dropdown menu however, when I navigate to any of the item on the menu the dropdown menu just disappears. I think I know the problem is that I have hover added on the button only, but how can I make it work like a normal dropdown ?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.dropdown {
display: inline-block;
padding: 5px;
/* background:#f1f1f1; */
}
.dropbtn {
padding: 5px;
margin-bottom: 0px;
width: 100%;
}
.dropdown-content {
display: none;
list-style: none;
background: #f1f1f1;
position: relative;
top: 0px;
left: 0px;
padding: 0px 0px 0px 0px;
margin: 0px;
text-align: center;
z-index: 1;
width: 100%;
}
.dropdown-content li {
margin-bottom: 0px;
padding: 3px;
}
.dropdown-content li a {
text-decoration: none;
color: black;
padding: 3px;
}
.dropdown-content li:hover,
.dropdown-content li a:hover {
text-decoration: none;
color: white;
background-color: #333;
}
.dropbtn:hover+.dropdown-content {
display: block;
}
<!-- language: lang-html -->
<div class="dropdown">
<button class="dropbtn">More</button>
<ul class="dropdown-content">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<!-- end snippet -->
I want the dropdown menu to stay visible while I am hovering over the child elements in the dropdown menu as any other dropdown menu works.
答案1
得分: 0
你可以直接在.dropdown
元素上设置:hover
属性,像这样:
.dropdown {
display: inline-block;
padding: 5px;
/*background:#f1f1f1; */
}
.dropbtn {
padding: 5px;
margin-bottom: 0px;
width: 100%;
}
.dropdown-content {
display: none;
list-style: none;
background: #f1f1f1;
position: relative;
top: 0px;
left: 0px;
padding: 0px 0px 0px 0px;
margin: 0px;
text-align: center;
z-index: 1;
width: 100%;
}
.dropdown-content li {
margin-bottom: 0px;
padding: 3px;
}
.dropdown-content li a {
text-decoration: none;
color: black;
padding: 3px;
}
.dropdown-content li:hover,
.dropdown-content li a:hover {
text-decoration: none;
color: white;
background-color: #333;
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="dropdown">
<button class="dropbtn">More</button>
<ul class="dropdown-content">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
英文:
You could set the :hover
property directly on the .dropdown
element like this :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.dropdown {
display: inline-block;
padding: 5px;
/*background:#f1f1f1; */
}
.dropbtn {
padding: 5px;
margin-bottom: 0px;
width: 100%;
}
.dropdown-content {
display: none;
list-style: none;
background: #f1f1f1;
position: relative;
top: 0px;
left: 0px;
padding: 0px 0px 0px 0px;
margin: 0px;
text-align: center;
z-index: 1;
width: 100%;
}
.dropdown-content li {
margin-bottom: 0px;
padding: 3px;
}
.dropdown-content li a {
text-decoration: none;
color: black;
padding: 3px;
}
.dropdown-content li:hover,
.dropdown-content li a:hover {
text-decoration: none;
color: white;
background-color: #333;
}
.dropdown:hover .dropdown-content {
display: block;
}
<!-- language: lang-html -->
<div class="dropdown">
<button class="dropbtn">More</button>
<ul class="dropdown-content">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<!-- end snippet -->
答案2
得分: 0
你在CSS中使用了hover
,这对于你的情况是正常行为。如果你想要点击按钮并显示下拉菜单,你将需要一些Javascript和一个onclick
事件:
function showDropdown() {
var content = document.getElementById("dropdown-content");
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
}
.dropdown {
display: inline-block;
padding: 5px;
}
.dropbtn {
padding: 5px;
margin-bottom: 0px;
width: 100%;
}
.dropdown-content {
display: none;
list-style: none;
background: #f1f1f1;
position: relative;
top: 0px;
left: 0px;
padding: 0px;
margin: 0px;
text-align: center;
z-index: 1;
width: 100%;
}
.dropdown-content li {
margin-bottom: 0px;
padding: 3px;
}
.dropdown-content li a {
text-decoration: none;
color: black;
padding: 3px;
}
.dropdown-content li:hover,
.dropdown-content li a:hover {
text-decoration: none;
color: white;
background-color: #333;
}
<div class="dropdown">
<button class="dropbtn" onclick="showDropdown()">More</button>
<ul class="dropdown-content" id="dropdown-content">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
英文:
You are using hover
in CSS, which is the normal behaviour for your case. If you want to click on the button, and display the dropdown you will need some Javascript and an onclick event :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function showDropdown() {
var content = document.getElementById("dropdown-content");
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
}
<!-- language: lang-css -->
.dropdown{
display:inline-block;
padding:5px;
/* background:#f1f1f1; */
}
.dropbtn{
padding:5px;
margin-bottom:0px;
width:100%;
}
.dropdown-content{
display:none;
list-style:none;
background:#f1f1f1;
position:relative;
top:0px;
left:0px;
padding: 0px 0px 0px 0px;
margin:0px;
text-align: center;
z-index:1;
width:100%;
}
.dropdown-content li{
margin-bottom:0px;
padding:3px;
}
.dropdown-content li a{
text-decoration:none;
color:black;
padding:3px;
}
.dropdown-content li:hover
,.dropdown-content li a:hover{
text-decoration:none;
color:white;
background-color:#333;
}
<!-- language: lang-html -->
<div class="dropdown">
<button class="dropbtn" onclick="showDropdown()">More</button>
<ul class="dropdown-content" id="dropdown-content">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论