如何在将光标移出按钮时避免下拉菜单消失?

huangapple go评论64阅读模式
英文:

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 -->

&lt;div class=&quot;dropdown&quot;&gt;
  &lt;button class=&quot;dropbtn&quot;&gt;More&lt;/button&gt;
  &lt;ul class=&quot;dropdown-content&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

<!-- 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 -->

&lt;div class=&quot;dropdown&quot;&gt;
  &lt;button class=&quot;dropbtn&quot;&gt;More&lt;/button&gt;
  &lt;ul class=&quot;dropdown-content&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

<!-- 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(&quot;dropdown-content&quot;);
  if (content.style.display === &quot;block&quot;) {
    content.style.display = &quot;none&quot;;
  } else {
    content.style.display = &quot;block&quot;;
  }
}

<!-- 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 -->

&lt;div class=&quot;dropdown&quot;&gt;
  &lt;button class=&quot;dropbtn&quot; onclick=&quot;showDropdown()&quot;&gt;More&lt;/button&gt;
  &lt;ul class=&quot;dropdown-content&quot; id=&quot;dropdown-content&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 18:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定