javascript - 带有子菜单的垂直菜单将显示在父菜单的高度内

标签 javascript html css drop-down-menu

我正在尝试创建一个垂直菜单,在悬停时显示一个子菜单。此外,子菜单必须保持在垂直菜单的高度范围内。我有点做到了

我希望当您将鼠标悬停在其父菜单上时,子菜单会停留片刻。我在上面放置了一个过渡延迟。但是当我将鼠标悬停在子菜单上时,它会显示垂直菜单的最后一个子菜单。我似乎找不到修复它的地方。

这是我的代码:

var checkBox = document.getElementById("label_check");
checkBox.addEventListener("mouseover", function(event) {

  setTimeout(function() {
    document.getElementById('label_check').click();
  }, 500);
}, false);
* {
  padding: 0;
  margin: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  background: #e9ecef;
  height: 1200px;
}

.menu-wrapper {
  width: 300px;
  margin: 20px auto;
  list-style-type: none;
  background: white;
  border-radius: 8px;
  -webkit-box-shadow: 0 10px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  box-shadow: 0 10px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.menu-wrapper .menu-header {
  height: 50px;
  line-height: 50px;
  background: #3a98f0;
  border-top-right-radius: 8px;
  border-top-left-radius: 8px;
  color: white;
  font-weight: bold;
  text-transform: uppercase;
}

.menu-wrapper .menu-header p {
  padding-left: 10px;
}

.menu-wrapper .menu-list .parent-menu {
  border-bottom-right-radius: 8px;
  border-bottom-left-radius: 8px;
  padding: 0 20px;
}

.menu-wrapper .menu-list ul {
  list-style-type: none;
  background: white;
}

.menu-wrapper .menu-list ul a {
  display: block;
  height: 100%;
  text-decoration: none;
  color: black;
  padding-left: 10px;
  position: relative;
  -webkit-transition: background .3s;
  transition: background .3s;
}

.menu-wrapper .menu-list ul.sub-menu {
  border-radius: 2px;
  -webkit-box-shadow: 0 10px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  box-shadow: 0 10px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.menu-wrapper .menu-list ul .has-sub>a:after {
  content: ' >';
  position: absolute;
  right: 10px;
}

.menu-wrapper .menu-list ul li.has-sub:first-child>ul.sub-menu {
  position: absolute;
  left: 290px;
  top: 0;
  padding-left: 5px;
  opacity: 0;
  width: 250px;
  transition: 0s .03s;
}

.menu-wrapper .menu-list ul li.has-sub:nth-last-child(-n+4)>ul.sub-menu {
  position: absolute;
  left: 290px;
  top: initial;
  bottom: -50px;
  padding-left: 5px;
  opacity: 0;
  width: 250px;
  transition: 0s .03s;
}

.menu-wrapper .menu-list ul li.has-sub:first-child>ul.sub-menu:before {
  content: '';
  display: block;
  width: 0;
  height: 0;
  position: absolute;
  border-top: 18px solid transparent;
  border-bottom: 18px solid transparent;
  border-right: 18px solid white;
  left: -12px;
  top: 10px;
}

.menu-wrapper .menu-list ul li.has-sub:nth-last-child(-n+4)>ul.sub-menu:before {
  content: '';
  display: block;
  width: 0;
  height: 0;
  position: absolute;
  border-top: 18px solid transparent;
  border-bottom: 18px solid transparent;
  border-right: 18px solid white;
  left: -12px;
  bottom: 55px;
}

.menu-wrapper .menu-list ul li.has-sub:first-child:hover>ul.sub-menu {
  opacity: 1;
  transition: 0s;
}

.menu-wrapper .menu-list ul li.has-sub:nth-last-child(-n+4):hover>ul.sub-menu {
  opacity: 1;
  transition: 0s;
}

.menu-wrapper .menu-list li {
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid #e9ecef;
  position: relative;
}

.menu-wrapper .menu-list li.grow ul.bottom-parent-menu>li:last-child {
  margin-bottom: 50px;
}

.menu-wrapper .menu-list .grow {
  position: relative;
  height: auto;
  border-top: 0;
  border-bottom-left-radius: 6px;
  border-bottom-right-radius: 6px;
}

.menu-wrapper .menu-list [type="checkbox"] {
  position: absolute;
  left: -9999px;
}

.menu-wrapper .menu-list [type="checkbox"]:checked~ul {
  display: block;
}

.menu-wrapper .menu-list [type="checkbox"]:checked+label {
  position: absolute;
  bottom: 0;
}

.menu-wrapper .menu-list [type="checkbox"]:checked+label:before {
  content: 'Less...';
  font-weight: 600;
}

.menu-wrapper .menu-list label {
  display: block;
  width: 100px;
  height: 50px;
  cursor: pointer;
  position: relative;
  background: white;
  border-bottom-left-radius: 8px;
  border-bottom-right-radius: 8px;
}

.menu-wrapper .menu-list label:before {
  position: absolute;
  content: 'More...';
  left: 10px;
  font-weight: 600;
}

.menu-wrapper .menu-list label:after {
  position: absolute;
}

.menu-wrapper .menu-list input[type="checkbox"]~ul {
  width: 100%;
  display: none;
}
<div class="menu-wrapper">
  <div class="menu-header">
    <p>&#9776; Categories</p>
  </div>
  <div class="menu-list">
    <ul class="parent-menu">
      <li class="has-sub">
        <a href="#">One</a>
        <ul class="sub-menu">
          <li><a href="#">Sub One</a></li>
          <li><a href="#">Sub Two</a></li>
          <li><a href="#">Sub Three</a></li>
          <li><a href="#">Sub Four</a></li>
        </ul>
      </li>
      <li><a href="#">Two</a></li>
      <li><a href="#">Three</a></li>
      <li class="has-sub">
        <a href="#">Four</a>
        <ul class="sub-menu">
          <li><a href="#">Sub One</a></li>
          <li><a href="#">Sub Two</a></li>
          <li><a href="#">Sub Three</a></li>
          <li><a href="#">Sub Four</a></li>
        </ul>
      </li>
      <li class="grow">
        <input type="checkbox" id="check_id">
        <label id="label_check" for="check_id"></label>
        <ul class="bottom-parent-menu">
          <li class="has-sub">
            <a href="#">Five</a>
            <ul class="sub-menu">
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
              <li><a href="#">Sub Four</a></li>
            </ul>
          </li>
          <li><a href="#">Six</a></li>
          <li><a href="#">Seven</a></li>
          <li class="has-sub">
            <a href="#">Eight</a>
            <ul class="sub-menu">
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
            </ul>
          </li>
          <li class="has-sub">
            <a href="#">Nine</a>
            <ul class="sub-menu">
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
            </ul>
          </li>
          <li class="has-sub">
            <a href="#">Ten</a>
            <ul class="sub-menu">
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>

最佳答案

因此,您必须将鼠标悬停在 display:block 而不是 opacity:1; 并将填充添加到 li 而不是 ul

var checkBox = document.getElementById("label_check");
checkBox.addEventListener("mouseover", function(event) {

  setTimeout(function() {
    document.getElementById('label_check').click();
  }, 500);
}, false);
* {
  padding: 0;
  margin: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  background: #e9ecef;
  height: 1200px;
}

.menu-wrapper {
  width: 300px;
  margin: 20px auto;
  list-style-type: none;
  background: white;
  border-radius: 8px;
  -webkit-box-shadow: 0 10px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  box-shadow: 0 10px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.menu-wrapper .menu-header {
  height: 50px;
  line-height: 50px;
  background: #3a98f0;
  border-top-right-radius: 8px;
  border-top-left-radius: 8px;
  color: white;
  font-weight: bold;
  text-transform: uppercase;
}

.menu-wrapper .menu-header p {
  padding-left: 10px;
}

.menu-wrapper .menu-list .parent-menu {
  border-bottom-right-radius: 8px;
  border-bottom-left-radius: 8px;
  padding: 0;
}

.menu-wrapper .menu-list ul {
  list-style-type: none;
  background: white;
}

.menu-wrapper .menu-list ul a {
  display: block;
  height: 100%;
  text-decoration: none;
  color: black;
  padding-left: 10px;
  position: relative;
  -webkit-transition: background .3s;
  transition: background .3s;
}

.menu-wrapper .menu-list ul.sub-menu {
  border-radius: 2px;
  -webkit-box-shadow: 0 10px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  box-shadow: 0 10px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.menu-wrapper .menu-list ul .has-sub>a:after {
  content: ' >';
  position: absolute;
  right: 10px;
}

.menu-wrapper .menu-list ul li.has-sub:first-child>ul.sub-menu {
  position: absolute;
  left: 310px;
  top: 0;
  padding-left: 5px;
  display: none;
  width: 250px;
  transition: 0s .03s;
}

.menu-wrapper .menu-list ul li.has-sub:nth-last-child(-n+4)>ul.sub-menu {
  position: absolute;
  left: 310px;
  top: initial;
  bottom: -50px;
  padding-left: 5px;
  display: none;
  width: 250px;
  transition: 0s .03s;
}

.menu-wrapper .menu-list ul li.grow {
  padding: 0;
}

.menu-wrapper .menu-list ul li.grow>ul li {
  padding: 0 40px !important;
}

.menu-wrapper .menu-list ul li.has-sub:first-child>ul.sub-menu:before {
  content: '';
  display: block;
  width: 0;
  height: 0;
  position: absolute;
  border-top: 18px solid transparent;
  border-bottom: 18px solid transparent;
  border-right: 18px solid white;
  left: -12px;
  top: 10px;
}

.menu-wrapper .menu-list ul li.has-sub:nth-last-child(-n+4)>ul.sub-menu:before {
  content: '';
  display: block;
  width: 0;
  height: 0;
  position: absolute;
  border-top: 18px solid transparent;
  border-bottom: 18px solid transparent;
  border-right: 18px solid white;
  left: -12px;
  bottom: 55px;
}

.menu-wrapper .menu-list ul li.has-sub:first-child:hover>ul.sub-menu {
  display: block;
  transition: 0s;
}

.menu-wrapper .menu-list ul li.has-sub:nth-last-child(-n+4):hover>ul.sub-menu {
  display: block;
  transition: 0s;
}

.menu-wrapper .menu-list li {
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid #e9ecef;
  position: relative;
  padding: 0 20px
}

.menu-wrapper .menu-list li.grow ul.bottom-parent-menu>li:last-child {
  margin-bottom: 50px;
}

.menu-wrapper .menu-list .grow {
  position: relative;
  height: auto;
  border-top: 0;
  border-bottom-left-radius: 6px;
  border-bottom-right-radius: 6px;
}

.menu-wrapper .menu-list [type="checkbox"] {
  position: absolute;
  left: -9999px;
}

.menu-wrapper .menu-list [type="checkbox"]:checked~ul {
  display: block;
}

.menu-wrapper .menu-list [type="checkbox"]:checked+label {
  position: absolute;
  bottom: 0;
}

.menu-wrapper .menu-list [type="checkbox"]:checked+label:before {
  content: 'Less...';
  font-weight: 600;
}

.menu-wrapper .menu-list label {
  display: block;
  width: 100px;
  height: 50px;
  cursor: pointer;
  position: relative;
  background: white;
  border-bottom-left-radius: 8px;
  border-bottom-right-radius: 8px;
}

.menu-wrapper .menu-list label:before {
  position: absolute;
  content: 'More...';
  left: 10px;
  font-weight: 600;
}

.menu-wrapper .menu-list label:after {
  position: absolute;
}

.menu-wrapper .menu-list input[type="checkbox"]~ul {
  width: 100%;
  display: none;
}
<div class="menu-wrapper">
  <div class="menu-header">
    <p>&#9776; Categories</p>
  </div>
  <div class="menu-list">
    <ul class="parent-menu">
      <li class="has-sub">
        <a href="#">One</a>
        <ul class="sub-menu">
          <li><a href="#">Sub One</a></li>
          <li><a href="#">Sub Two</a></li>
          <li><a href="#">Sub Three</a></li>
          <li><a href="#">Sub Four</a></li>
        </ul>
      </li>
      <li><a href="#">Two</a></li>
      <li><a href="#">Three</a></li>
      <li class="has-sub">
        <a href="#">Four</a>
        <ul class="sub-menu">
          <li><a href="#">Sub One</a></li>
          <li><a href="#">Sub Two</a></li>
          <li><a href="#">Sub Three</a></li>
          <li><a href="#">Sub Four</a></li>
        </ul>
      </li>
      <li class="grow">
        <input type="checkbox" id="check_id">
        <label id="label_check" for="check_id"></label>
        <ul class="bottom-parent-menu">
          <li class="has-sub">
            <a href="#">Five</a>
            <ul class="sub-menu">
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
              <li><a href="#">Sub Four</a></li>
            </ul>
          </li>
          <li><a href="#">Six</a></li>
          <li><a href="#">Seven</a></li>
          <li class="has-sub">
            <a href="#">Eight</a>
            <ul class="sub-menu">
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
            </ul>
          </li>
          <li class="has-sub">
            <a href="#">Nine</a>
            <ul class="sub-menu">
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
            </ul>
          </li>
          <li class="has-sub">
            <a href="#">Ten</a>
            <ul class="sub-menu">
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
              <li><a href="#">Sub Three</a></li>
              <li><a href="#">Sub One</a></li>
              <li><a href="#">Sub Two</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>

jsfiddle

关于javascript - 带有子菜单的垂直菜单将显示在父菜单的高度内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48604694/

相关文章:

javascript - toDataURL 不是函数

javascript - 基于哪个条件打破了 Javascript 中的 while 循环,最干净的分支方法是什么?

javascript - 变量值正在打印 '[ object Object]'

html - 仅适合一个全屏的背景图片

html - Ruby on Rails HTML 页面的图像和样式表放在哪个目录?

javascript - 将 JavaScript 字符串转换为 JavaScript 文字

html - 在wordpress post中添加默认代码

javascript - 如何在 PHP 中使用自定义购物车

javascript - 如何在此脚本中添加更多标题?

css - 是否可以将不同的样式应用于word中的不同字母?