子菜单在从菜单移动到子菜单时消失

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

Submenu disappear when moving from menu to submenu

问题

根据我的代码,如何修改CSS以解决问题(从菜单移到子菜单再到子子菜单)。我刚刚更新了我的问题以使其更清楚。

I have the following code, I'm stuck on how to move the mouse from the menu to the submenu (the submenu always disappears when I tried doing so).

根据我的代码,我在如何将鼠标从菜单移动到子菜单上遇到了问题(当我尝试这样做时,子菜单总是消失)。

英文:

I have the following code, I'm stuck on how to move the mouse from the menu to the submenu (the submenu always disappears when I tried doing so).

I did not use <ul\> and <li\> in my code and not sure if some little modify can solve this problem... I feel like I did not figure out the relationship between the parent and the child (menu), but I have no clue about how to deal with it. Thanks a lot!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.dropdown .dropbtn {
        border: none;
        outline: none;
        color: #0f4391;
        padding-top: 8.5px;
        padding-right: 15px;
        padding-bottom: 8.5px;
        padding-left: 15px;
        background-color: inherit;
        font-family: inherit;
      }

      .dropdown-content,
      .sub-1 {
        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;
      }

      .dropdown-content a,
      .sub-1 a {
        float: none;
        color: #0f4391;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
        text-align: left;
      }

      .dropbtn:hover {
        background-color: lightgray;
        color: black;
      }

      .dropdown:hover .dropdown-content {
        display: block;
      }


      /* To let css detect next element of hovered element to take action */
      .dropdown .dropdown-content a:nth-child(1):hover + .sub-1 {
        background-color: hotpink;
        display: block;
        margin-left: 10em;
        margin-top : -2.5em;
      }

<!-- language: lang-html -->

&lt;div class=&quot;dropdown&quot; id=&quot;myDropdown&quot;&gt;
      &lt;!--  --&gt;
      &lt;button class=&quot;dropbtn&quot;&gt;
        &lt;i style=&quot;font-size: 24px&quot; class=&quot;fa&quot;&gt;&lt;/i&gt; level_0
        &lt;i class=&quot;fa fa-caret-down&quot;&gt;&lt;/i&gt;
      &lt;/button&gt;
      &lt;!--  --&gt;
      &lt;div class=&quot;dropdown-content&quot;&gt;
        &lt;!-- Logic : div after element (to be hovered) to detect for action--&gt;
        &lt;a href=&quot;#&quot;&gt;level_1&lt;/a&gt;
        &lt;div class=&quot;sub-1&quot;&gt;
          &lt;a href=&quot;#&quot;&gt;level_1-1&lt;/a&gt;
          &lt;a href=&quot;#&quot;&gt;level_1-2&lt;/a&gt;
          &lt;a href=&quot;#&quot;&gt;level_1-3&lt;/a&gt;
        &lt;/div&gt;
        &lt;!--  --&gt;
        &lt;a href=&quot;#&quot;&gt;level_2&lt;/a&gt;
        &lt;a href=&quot;#&quot;&gt;level_3&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

Based on my code, how to modify CSS to solve the problem (moving from menu to submenu to sub submenu. I just updated my question to make it clear.

答案1

得分: 0

好的,以下是您要求的内容的中文翻译:

问题是这样的,只有在鼠标悬停在父菜单上时,您才显示子菜单,当您从父菜单移动到子菜单时,父菜单不再处于悬停状态,子菜单消失。

请尝试使用以下CSS:

.dropdown .dropbtn {
  /* 您的样式 */
}

.dropdown-content,
.sub-1,
.sub-2 { /* 为二级子菜单添加新类 */
  display: none;
  /* 您的样式 */
}

/* 当鼠标悬停在父菜单上时显示第一级子菜单 */
.dropdown:hover .dropdown-content {
  display: block;
}

/* 当鼠标悬停在其父菜单或子菜单本身时显示第二级子菜单 */
.sub-1:hover .sub-2,
.sub-2:hover {
  display: block;
}

/* 鼠标悬停效果、链接等的样式 */

并使用以下HTML代码修改您的div嵌套结构:

<div class="dropdown" id="myDropdown">
  <button class="dropbtn">
    <!-- 您的按钮内容 -->
  </button>
  <div class="dropdown-content">
    <a href="#">level_1</a>
    <div class="sub-1">
      <a href="#">level_11</a>
      <div class="sub-2">
        <a href="#">level_111</a>
        <a href="#">level_112</a>
      </div>
      <a href="#">level_12</a>
      <a href="#">level_13</a>
    </div>
    <a href="#">level_1</a>
    <a href="#">level_1</a>
  </div>
</div>

请注意,这是您提供的CSS和HTML代码的中文翻译。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

Ok, the problem is the following, you are showing the submenu only when the parent menu is being hovered and when you move from the parent menu to the submenu, the parent menu is no longer being hovered and the submenu disappears.

try with this CSS

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.dropdown .dropbtn {
  /* your styles */
}

.dropdown-content,
.sub-1,
.sub-2 { /* add a new class for the second-level submenu */
  display: none;
  /* your styles */
}

/* show the first-level submenu when parent is being hovered */
.dropdown:hover .dropdown-content {
  display: block;
}

/* show the second-level submenu when either its parent or itself is being hovered */
.sub-1:hover .sub-2,
.sub-2:hover {
  display: block;
}

/* your styles for hover effects, links etc.. */

<!-- end snippet -->

and modify your HTML div nest with this code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div class=&quot;dropdown&quot; id=&quot;myDropdown&quot;&gt;
  &lt;button class=&quot;dropbtn&quot;&gt;
    &lt;!-- your button content --&gt;
  &lt;/button&gt;
  &lt;div class=&quot;dropdown-content&quot;&gt;
    &lt;a href=&quot;#&quot;&gt;level_1&lt;/a&gt;
    &lt;div class=&quot;sub-1&quot;&gt;
      &lt;a href=&quot;#&quot;&gt;level_11&lt;/a&gt;
      &lt;div class=&quot;sub-2&quot;&gt;
        &lt;a href=&quot;#&quot;&gt;level_111&lt;/a&gt;
        &lt;a href=&quot;#&quot;&gt;level_112&lt;/a&gt;
      &lt;/div&gt;
      &lt;a href=&quot;#&quot;&gt;level_12&lt;/a&gt;
      &lt;a href=&quot;#&quot;&gt;level_13&lt;/a&gt;
    &lt;/div&gt;
    &lt;a href=&quot;#&quot;&gt;level_1&lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;level_1&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

好的,以下是翻译好的部分:

Okay, I think I finally solved the problem by figuring out the relationships between parent and child div when hovering. Also, it seems like the part of menu and submenu should be overlapped (at least no gap).

在这里,我认为我最终通过弄清楚在悬停时父元素和子元素之间的关系来解决了问题。另外,菜单和子菜单的部分应该重叠在一起(至少没有间隙)。

#myDropdown {
    display: inline-block;
    vertical-align: -3.3px;
}

.dropdown .dropbtn {
  border: none;
  outline: none;
  color: #0F4391;
  padding-top: 8.5px;
  padding-right: 15px;
  padding-bottom: 8.5px;
  padding-left: 15px;
  background-color: inherit;
  font-family: inherit;
}

.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;
}

.sub-1 {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 279px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a, .sub-1 a {
  float: none;
  color: #0F4391;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.w3.my-nav a:hover, .dropdown:hover .dropbtn {
  background-color: lightgray;
  color: black;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.sub-1:hover, .dropdown .dropdown-content a:nth-child(3):hover + .sub-1 {
        background-color: inherit;
        display: inherit;
        margin-left: 8em;
        margin-top: -2.5em;
}

<div class="dropdown" id="myDropdown">
  <button class="dropbtn">
    Main Menu
  </button>
  <div class="dropdown-content">
    <a href="#">Level1_1</a>
    <a href="#">Level1_2</a>
    <a href="#">Level1_3</a>
    <div class="sub-1" id="sub-1">
      <a href="#">SubLevel1_3-1</a>
      <a href="#">SubLevel1_3-2</a>
    </div>
  </div>
</div>
英文:

Okay, I think I finally solved the problem by figuring out the relationships between parent and child div when hovering. Also, it seems like the part of menu and submenu should be overlapped (at least no gap).

.sub-1:hover, .dropdown .dropdown-content a:nth-child(3):hover +
.sub-1 {
        background-color: inherit;
        display: inherit;
        margin-left: 8em;
        margin-top: -2.5em; }

Here is the updated css and html. Thanks!
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#myDropdown {
    display: inline-block;
    vertical-align: -3.3px;
}

.dropdown .dropbtn {
  border: none;
  outline: none;
  color: #0F4391;
  padding-top: 8.5px;
  padding-right: 15px;
  padding-bottom: 8.5px;
  padding-left: 15px;
  background-color: inherit;
  font-family: inherit;
}

.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;
}

.sub-1 {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 279px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a, .sub-1 a {
  float: none;
  color: #0F4391;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.w3.my-nav a:hover, .dropdown:hover .dropbtn {
  background-color: lightgray;
  color: black;
}


.dropdown:hover .dropdown-content {
  display: block;
}

.sub-1:hover, .dropdown .dropdown-content a:nth-child(3):hover + .sub-1 {
        background-color: inherit;
        display: inherit;
        margin-left: 8em;
        margin-top: -2.5em;
}

<!-- language: lang-html -->

&lt;div class=&quot;dropdown&quot; id=&quot;myDropdown&quot;&gt;
  &lt;button class=&quot;dropbtn&quot;&gt;
    Main Menu
  &lt;/button&gt;
  &lt;div class=&quot;dropdown-content&quot;&gt;
    &lt;a href=&quot;#&quot;&gt;Level1_1&lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;Level1_2&lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;Level1_3&lt;/a&gt;
    &lt;div class=&quot;sub-1&quot; id=&quot;sub-1&quot;&gt;
      &lt;a href=&quot;#&quot;&gt;SubLevel1_3-1&lt;/a&gt;
      &lt;a href=&quot;#&quot;&gt;SubLevel1_3-2&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定