隐藏动画侧边栏中的子菜单

问题描述 投票:0回答:3

我正在尝试使用CSS和JS创建动画侧边栏,并且隐藏子菜单(demo中为绿色)时遇到问题。因此,子菜单应该像现在一样缓慢地飞出,但是仅当父级<li>悬停时。当前,它们始终存在。另外,悬停在<li>下方的列表项目应适当向下移动。在这种情况下,当“交易”悬停时,交易子菜单应缓慢出现并占用空间,因此“预算”相应地向下移动。我已经尝试过使用display: none;display: block;进行某些操作,但是我已经读到动画无法在display属性上运行,并且无法使其正常工作,我真的很想保留动画。我也尝试过使用visibility进行某些操作,但是失败了,不值得一提。

document.addEventListener('DOMContentLoaded', () => {
  let listItems = document.querySelectorAll('nav > ul > li');

  function onListItemClick() {
    for (let listItem of listItems) {
      listItem.classList.remove('active');
      listItem.children[0].classList.remove('dot');
      if (listItem.children.length > 2) {
        listItem.children[2].style.display = 'none';
      }
    }
    this.classList.add('active');
    this.children[0].classList.add('dot');
    if (this.children.length > 2) {
      this.children[2].style.display = 'block';
    }
  }
  listItems.forEach(function(listItem) {
    listItem.addEventListener('click', onListItemClick);
  });
});
.sidebar {
  background-color: #F2F2F2;
  width: 300px;
  height: 100%;
  position: fixed;
  font-family: sans-serif;
}

.sidebar>nav>ul {
  list-style: none;
  line-height: 2em;
  padding: 0;
  margin: 10em 0 0 0;
}

.sidebar>nav>ul>li {
  display: grid;
  grid-template-columns: 25px 1fr;
  color: #959595;
  font-size: 18px;
}

.sidebar>nav>ul>li>span:nth-child(1) {
  margin-left: 3em;
}

.sidebar>nav>ul>li>span:nth-child(2) {
  padding: 0.5em 0 0.5em 3em;
}

.sidebar>nav>ul>li:hover {
  color: #757575;
  font-weight: bold;
  cursor: pointer;
}

.sidebar>nav>ul>li>ul {
  z-index: 9;
  position: absolute;
  list-style: none;
  padding: 0;
  margin: 0;
  width: 300px;
  transform: translateY(0);
  transition: all 0.5 ease;
}

.sidebar>nav>ul>li:hover>ul {
  transform: translateY(30%);
  transition: all 0.5s ease;
}

.sidebar>nav>ul>li>ul>li {
  display: block;
  box-sizing: border-box;
  background-color: #33ad93;
  font-size: 14px;
  width: 100%;
  font-weight: normal;
  padding: 0 0 0 5.5em;
}

.sidebar>nav>ul>li>ul>li:first-child {
  padding: 0.5em 0 0 5.5em;
}

.sidebar>nav>ul>li>ul>li:last-child {
  padding: 0 0 0.5em 5.5em;
}

.sidebar>nav>ul>li>ul>li:not(:first-child):not(:last-child) {
  padding: 0.15em 0 0.15em 5.5em;
}

.sidebar>nav>ul>li>ul>li>a {
  color: white;
}

.sidebar>nav>ul>li>span>a {
  color: #959595;
  text-decoration: none;
}

.dot {
  border-radius: 50%;
  background-color: #33ad93;
  width: 10px;
  height: 10px;
  display: inline-block;
  margin: auto;
}

.active {
  color: black !important;
  font-weight: bold;
}
<div class="sidebar">
  <nav>
    <ul>
      <li class=" active ">
        <span class=" dot "></span>
        <span><a href="/dashboard/">Dashboard</a></span>
      </li>
      <li class="">
        <span class=""></span>
        <span>Transactions</span>
        <ul>
          <li><a href="/transactions/create/">New Transaction</a></li>
          <li>View Transactions</li>
        </ul>
      </li>
      <li class="">
        <span class=""></span>
        <span>Budgets</span>
      </li>
    </ul>
  </nav>
</div>

我该如何解决这个问题?

javascript css
3个回答
0
投票

我做了一些小的更改,似乎完成了您想要的:

.sidebar > nav > ul > li > ul {
    z-index: 9;
    /*position: absolute;  *removed*/
    height: 0; /*added*/
    list-style: none;
    padding: 0;
    margin: 0;
    width: 300px;
    /*transform: translateY(0);  *removed*/
    /*transition: all 0.5 ease;  *removed*/
    overflow-y: hidden; /*added*/
}

.sidebar > nav > ul > li:hover > ul {
    /*transform: translateY(30%);  *removed*/
    height: 80px; /*added*/
    transition: height 0.5s ease; /*modified*/
}

.sidebar > nav > ul > li > ul > li {
    height: 40px; /*added*/
    display: block;
    box-sizing: border-box;
    background-color: #33ad93;
    font-size: 14px;
    width: 100%;
    font-weight: normal;
    padding: 0 0 0 5.5em;
}

因此,我向新下拉列表的每个列表项添加了一个静态高度为40px,然后将容器设置为80px。然后,我们转换高度而不是Y位置。如果您希望它相对于元素数是动态的,那么不幸的是,它不如使用height: 100%而不是height: 80px容易(您需要使用一些JavaScript来动态设置相对于列表项数的高度) ;它仍然可以使用,但过渡效果会稍有改变)。

希望这应该能给您一个想法


0
投票

您刚刚错过了默认ul上的过渡单位。

这里是更新的代码

document.addEventListener('DOMContentLoaded', () => {
  let listItems = document.querySelectorAll('nav > ul > li');
  function onListItemClick() {
    for (let listItem of listItems) {
      listItem.classList.remove('active');
      listItem.children[0].classList.remove('dot');
      if (listItem.children.length > 2) {
        listItem.children[2].style.display = 'none';
      }
    }
    this.classList.add('active');
    this.children[0].classList.add('dot');
    if (this.children.length > 2) {
      this.children[2].style.display = 'block';
    }
  }
  listItems.forEach(function(listItem) {
    listItem.addEventListener('click', onListItemClick);
  });
});
.sidebar {
    background-color: #F2F2F2;
    width: 300px;
    height: 100%;
    position: fixed;
    font-family: sans-serif;
}

.sidebar > nav > ul {
    list-style: none;
    line-height: 2em;
    padding: 0;
    margin: 10em 0 0 0;
}

.sidebar > nav > ul > li {
    display: grid;
    grid-template-columns: 25px 1fr;
    color: #959595;
    font-size: 18px;
}

.sidebar > nav > ul > li > span:nth-child(1) {
    margin-left: 3em;
}

.sidebar > nav > ul > li > span:nth-child(2) {
    padding: 0.5em 0 0.5em 3em;
}

.sidebar > nav > ul > li:hover {
    color: #757575;
    font-weight: bold;
    cursor: pointer;
}

.sidebar > nav > ul > li > ul {
    z-index: 0;
    height: 0px;
    overflow-y: hidden;
    list-style: none;
    padding: 0;
    margin: 0;
    width: 300px;
    transform: translateY(0);
    transition: all 0.5s linear;
}

.sidebar > nav > ul > li:hover > ul {
    height: 100%;
    transform: translateY(0);
}

.sidebar > nav > ul > li > ul > li {
    display: block;
    box-sizing: border-box;
    background-color: #33ad93;
    font-size: 14px;
    width: 100%;
    font-weight: normal;
    padding: 0 0 0 5.5em;
}

.sidebar > nav > ul > li > ul > li:first-child {
    padding: 0.5em 0 0 5.5em;
}

.sidebar > nav > ul > li > ul > li:last-child {
    padding: 0 0 0.5em 5.5em;
}

.sidebar > nav > ul > li > ul > li:not(:first-child):not(:last-child) {
    padding: 0.15em 0 0.15em 5.5em;
}

.sidebar > nav > ul > li > ul > li > a {
    color: white;
    text-decoration: none;
}

.sidebar > nav > ul > li > ul > li > a:hover {
    text-decoration: underline;
}

.sidebar > nav > ul > li > span > a {
    color: inherit;
    text-decoration: none;
}

.dot {
    border-radius: 50%;
    background-color: #33ad93;
    width: 10px;
    height: 10px;
    display: inline-block;
    margin: auto;
}

.active {
    color: black !important;
    font-weight: bold;
}
<div class="sidebar">
  <nav>
    <ul>
      <li class=" active ">
        <span class=" dot "></span>
        <span><a href="/dashboard/">Dashboard</a></span>
      </li>
      <li class="">
        <span class=""></span>
        <span>Transactions</span>
        <ul>
          <li><a href="/transactions/create/">New Transaction</a></li>
          <li>View Transactions</li>
        </ul>
      </li>
      <li class="">
        <span class=""></span>
        <span>Budgets</span>
      </li>
    </ul>
  </nav>
</div>

0
投票

您可能想查看CSS Sidebars上的W3.CSS Framework页面。

下面的代码段是使用该框架的示例。然后,您可以从那里将其调整为您的样式。

<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"/>
<div class="w3-sidebar w3-bar-block w3-light-grey w3-card">
  <a href="#" class="w3-bar-item w3-button">Dashboard</a>
  <div class="w3-dropdown-hover">
    <button class="w3-button">Transactions
                <span style="font-size:12px;padding-bottom:2px">&#9660;</span>
            </button>
    <div class="w3-dropdown-content w3-bar-block" style="box-shadow: 0 0.25em 0.5em #00000040;">
      <a href="#" class="w3-bar-item w3-button">New Transaction</a>
      <a href="#" class="w3-bar-item w3-button">View Transactions</a>
    </div>
  </div>
  <a href="#" class="w3-bar-item w3-button">Budgets</a>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.