CSS 过渡适用于 li 元素但不适用于 div

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

我正在使用 CSS 和 vanilla javascript 构建一个可点击的响应式下拉菜单。它的工作效率为 99.99% - 可能足以使用 - 但引擎盖下发生了一些奇怪的事情。我想让下拉菜单动画进出,它在扩展模式下工作正常——但在汉堡包模式下,动画不会发生;它只是在出现或消失之前暂停过渡的长度。

下拉容器目前是 div。如果我将它们更改为 li,它就解决了问题……但由于在扩展模式下破坏了下拉菜单的定位而造成了更严重的问题——它们都在最左侧的菜单项下对齐。 (关于下拉列表的定位有一些严重的问题;出于我不明白的原因,我不得不将它们更改为 position:fixed 而不是 absolute 以使它们对齐,并且必须将它们定位在 top:60px 因为它们是与页面顶部齐平。所有这些都可能与我必须在展开模式下将菜单项容器更改为 flexbox 以使顶级菜单项完全正确布局这一事实有关我是 flexboxes 的新手。尽管出于美学原因我也需要它成为 flexbox,所以我可以在扩展模式下调整链接的顺序。)

这是一个下拉容器是 div 的演示,这是一个下拉容器是 li 的演示。下面的代码。

菜单HTML:

<nav>

  <div class="logo">

<a class="ourname" href="#">
<img src="http://www.sacredfools.org/images/2d-logo-white-on-black-500px-128x128.png">
SACRED FOOLS</a></div>

    <input type="checkbox" id="menutoggler" class="toggler">
    <div class="hamburger"><div></div></div>

  <ul class="menu__box">

    <li><a class="menu__item" href="#">TICKETS</a></li>

    <li><a class="menu__item shows__dropbtn" id="shows__button" onclick="shows__dropdown()">SHOWS
             <i class="fa-solid fa-caret-down"></i></a>

        <li class="shows__dropdownhider" id="shows__dropdowncontainer">
           <a class="menu__dropdownitem" href="#">All Shows</a>
           <a class="menu__dropdownitem" href="#">Calendar</a>
           <a class="menu__dropdownitem menu__lastdropdown" href="#">Past Productions</a>
        </li></li>

    <li><a class="menu__item more__dropbtn" id="more__button" onclick="more__dropdown()">MORE
             <i class="fa-solid fa-caret-down"></i></a>

        <li class="more__dropdownhider" id="more__dropdowncontainer">
           <a class="menu__dropdownitem" href="#">Directions</a>
           <a class="menu__dropdownitem" href="#">Members & Staff</a>
           <a class="menu__dropdownitem" href="#">Get Involved</a>
           <a class="menu__dropdownitem" href="#">About</a>
           <a class="menu__dropdownitem" href="#">Mission</a>
           <a class="menu__dropdownitem" href="#">Anti-Racism</a>
           <a class="menu__dropdownitem" href="#">EDI</a>
           <a class="menu__dropdownitem" href="#">Education & Outreach</a>
           <a class="menu__dropdownitem" href="#">Press</a>
           <a class="menu__dropdownitem" href="#">Awards</a>
           <a class="menu__dropdownitem" href="#">Contact Us</a>
           <a class="menu__dropdownitem" href="#">Mailing List</a>
           <a class="menu__dropdownitem menu__lastdropdown" href="#">Search</a>
        </li></li>

    <li><a class="menu__item menu__highlight menu__lastitem" href="#">DONATE</a></li>

        <li><a class="menu__item menu__social" href="#"><i class="fa-brands fa-square-facebook"></i></a></li>
        <li><a class="menu__item menu__social" href="#"><i class="fa-brands fa-square-twitter"></i></a></li>
        <li><a class="menu__item menu__social" href="#"><i class="fa-brands fa-square-instagram"></i></a></li>

        <li class="menu__overflowspacer"></li>

    </ul>
  
</nav>

用于下拉菜单的 Javascript - 应用和删除用于显示和隐藏下拉菜单的类(以及用于突出显示顶部菜单项的额外类):

<script>

/* SHOWS Dropdown */

function shows__dropdown() {
  document.getElementById("shows__dropdowncontainer").classList.toggle("menu__dropdownshow");
  document.getElementById("shows__button").classList.toggle("menu__toggledon");
}


/* MORE Dropdown */

function more__dropdown() {
  document.getElementById("more__dropdowncontainer").classList.toggle("menu__dropdownshow");
  document.getElementById("more__button").classList.toggle("menu__toggledon");
}

/* Close dropdowns when clicking outside */

window.onclick = function(e) {

  if (!e.target.matches('.shows__dropbtn')) {
  var shows__dropdowncontainer = document.getElementById("shows__dropdowncontainer");
    if (shows__dropdowncontainer.classList.contains('menu__dropdownshow')) {
      shows__dropdowncontainer.classList.remove('menu__dropdownshow');
    }
  }

  if (!e.target.matches('.shows__dropbtn')) {
  var shows__button = document.getElementById("shows__button");
    if (shows__button.classList.contains('menu__toggledon')) {
      shows__button.classList.remove('menu__toggledon');
    }
  }

  if (!e.target.matches('.more__dropbtn')) {
  var more__dropdowncontainer = document.getElementById("more__dropdowncontainer");
    if (more__dropdowncontainer.classList.contains('menu__dropdownshow')) {
      more__dropdowncontainer.classList.remove('menu__dropdownshow');
    }
  }


  if (!e.target.matches('.more__dropbtn')) {
  var more__button = document.getElementById("more__button");
    if (more__button.classList.contains('menu__toggledon')) {
      more__button.classList.remove('menu__toggledon');
    }
  }

}

</script>

菜单CSS:

nav {background-color: #282828;
     background-image: url("stagefog-lights.jpg");
     background-image: url("stagelights.jpg");
     background-image: url("litfog.jpg");
     padding: 0; margin: 0;
     position: fixed; top: 0; left: 0;
     width: 100%; z-index: 2;
}

nav, .menuspacer {height: 60px}

.logo {color: #ffffff; font-size: 12pt; position: fixed; top: 0; left: 0;
   font-family: 'Montserrat', sans-serif; font-weight: bold; z-index: 3;
     text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.7);
   }

.logo img {height: 60px; margin-left: 0px; margin-right: 5px; margin-top: 0px; margin-bottom: 0px;
 vertical-align:middle;}

a.ourname:link, a.ourname:visited, a.ourname:hover, a.ourname:active
 {text-decoration: none; color: #ffffff;}

.toggler{
  /* ALWAYS KEEPING THE TOGGLER OR THE CHECKBOX ON TOP OF EVERYTHING :  */
    z-index:2;
    height: 50px;
    width: 50px;
    position: absolute;
    top: 0;
    right: 0;
    cursor: pointer;
    opacity: 0;
}

.hamburger{
    position: absolute;
    top: 0;
    right: 0;
    height: 40px;
    width: 40px;
    padding: 0.6rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.hamburger > div{
    position: relative;
    top: 0;
    left: 0;
    background: white;
    height: 2px;
    width: 60%;
    transition: all  0.4s ease;
}

.hamburger > div::before,
.hamburger > div::after{
    content: '';
    position: absolute;
    top: -8px;
    background: white;
    width: 100%;
    height: 2px;
    transition: all  0.4s ease;
}

.hamburger > div::after{
    top: 8px;
}

.toggler:checked + .hamburger > div{
    background: rgba(0,0,0,0);
}

.toggler:checked + .hamburger > div::before{
    top: 0;
    transform: rotate(45deg);
    background: white;
}

.toggler:checked + .hamburger > div::after{
    top: 0;
    transform: rotate(135deg);
    background: white;
}

.toggler:checked ~ .menu__box{
    left: 0px;
}

.menu__box {
  position: fixed;
  top: 60px;
  left: -100%;
  width: 280px;
  height: 100%;
  min-height: 100%;
  box-sizing: border-box;
  margin: 0px;
  padding-left: 0;
  list-style: none;
  background-color: #282828;
  transition: all 0.4s ease;
  overflow: auto;
  z-index: 1;
}

.menu__item {
  display: block; float: none;
  padding: 11px;
  color: #ffffff;
  font-family: 'Open Sans', sans-serif;
  font-size: 12pt;
  font-weight: 600;
  text-decoration: none;
  transition-duration: .25s;
  border-top: 1px dotted #ffffff;
}

.menu__dropbox {list-style: none;}

.menu__dropdownitem {
  display: block;
  padding-top: 5px;
  padding-left: 30px;
  padding-right: 11px;
  padding-bottom: 5px;
  color: #ffffff;
  font-family: 'Open Sans', sans-serif;
  font-size: 12pt;
  font-weight: 600;
  text-decoration: none;
  transition-duration: .25s;
}

.shows__dropdownhider, .more__dropdownhider {max-height: 0; overflow: hidden;
   -webkit-transition-duration: .25s;
   -moz-transition-duration: .25s;
   -o-transition-duraton: .25s;
   transition-duration: .25s;}

.menu__dropdownshow {max-height: 100%;}

.shows__dropbtn, .more__dropbtn {cursor: pointer;}

.menu__lastdropdown {padding-bottom: 11px}

.menu__lastitem {border-bottom: 1px dotted #ffffff;}

.menu__item:hover, .menu__item:active,
.menu__dropdownitem:hover, .menu__dropdownitem:active {color: #FB0901}

.menu__item:hover, .menu__item:active,
.menu__dropdownitem:hover, .menu__dropdownitem:active {background-color: #000000}

.menu__highlight {background-color: #404040}

.menu__social {float: left; border: none; font-size: 20pt}

.menu__social:hover, .menu__social:active {background-color: transparent}

.menu__overflowspacer {height: 120px}

.fa-caret-down {margin: 5px; font-size: 8pt; float: right}

.menu__toggledon {color: #FB0901; background-color: #000000;}



@media (min-width: 768px) {

.hamburger {display: none}

.toggler:checked ~ .menu__box{
    left: auto;
}

.menu__box {
  display: flex;
  align-items: center;
  flex-direction: row;
  position: fixed;
  top: 0px;
  right: 0px;
  left: auto;
  width: auto;
  height: 60px;
  min-height: auto;
  box-sizing: content-box;
  margin: 0;
  padding: 0;
  list-style: none;
  box-shadow: none;
  overflow: hidden;
  background-color: transparent;
  z-index: 3;
  margin: 0;
  transition: all 0s ease;
}

.menu__item {
  padding-left: 15px; padding-right: 15px;
  padding-top: 0px; padding-bottom: 0px;
  margin: 0px;
  font-family: 'Open Sans Condensed', sans-serif;
  font-size: 12pt;
  font-weight: 700;
  text-decoration: none;
  transition-duration: .25s;
  border-top: none;
  padding-top: 30%; padding-bottom: 30%;
  text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.7);
}

.menu__social {font-size: 15pt; padding-left: 10px; padding-right: 10px; padding-top: 0px; padding-bottom: 0px; margin: 0px;}

.final__social {padding-right: 15pt}

.menu__social:hover, .menu__social:active {text-shadow: 1px 1px 1px rgba(255,255,255, .75);}

.menu__dropdownitem {
  display: block;
  padding-top: 5px;
  padding-left: 11px;
  padding-right: 11px;
  padding-bottom: 5px;
  color: #ffffff;
  font-family: 'Open Sans Condensed', sans-serif;
  font-size: 12pt;
  font-weight: 700;
  text-decoration: none;
  transition-duration: .25s;
  background-color: #000000;
}

.shows__dropbtn, .more__dropbtn {position: relative;}

.shows__dropdownhider, .more__dropdownhider {
  position: fixed; top: 60px; background-color: #282828;
 }

.menu__lastitem {border-bottom: none;}

.menu__overflowspacer {height: 0px}

.fa-caret-down {margin-left: 5px; font-size: 12pt; float: none;
 padding: 0px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px}

.menu__highlight {background-color: transparent;
   font-family: 'Roboto', sans-serif; font-size: 9pt; font-weight: 500;
   border: 1px solid #ffffff;
   padding: 10px; margin-left: 10px; margin-right: 10px;
   border-radius: 5px; box-shadow: 2px 2px 6px rgba(0,0,0,0.5);
   text-shadow: none;}

.menu__highlight:hover, .menu__highlight:active {background-color: #ffffff; color: #000000;}

.menu__box :nth-child(6) {order: 1;}

}
drop-down-menu responsive-design css-transitions html-lists
© www.soinside.com 2019 - 2024. All rights reserved.