css 如何使用媒体查询关闭下拉菜单?

az31mfrm  于 2023-03-24  发布在  其他
关注(0)|答案(2)|浏览(130)

(Jsfiddle用于上下文:https://jsfiddle.net/hfwtq2c9/12/
我做了一个导航栏,为了简洁,一些链接在下拉菜单中。当屏幕太小时,它会将所有链接折叠成一个菜单,可以通过一个按钮访问。我想让它进入菜单时,所有链接都从下拉菜单中出来,进入一个列表。我还有一个问题,下拉菜单中的链接在左边有一些随机的空白,我不知道为什么会这样。这可以做到吗?谢谢
我的代码:

const primaryNav = document.querySelector(".primary-navigation");
const navToggle = document.querySelector(".mobile-nav-toggle");

navToggle.addEventListener("click", () => {
  const visibility = primaryNav.getAttribute("data-visible");

  if (visibility === "false") {
    primaryNav.setAttribute("data-visible", true);
    navToggle.setAttribute("aria-expanded", true);
  } else if (visibility === "true") {
    primaryNav.setAttribute("data-visible", false);
    navToggle.setAttribute("aria-expanded", false);
  }
});
@import url("https://fonts.googleapis.com/css2?family=Poppins&display=swap");

* {
  font-family: "Poppins", sans-serif;
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  background: radial-gradient(circle,
      rgba(0, 102, 215, 1) 0%,
      rgba(15, 0, 157, 1) 100%);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#0066d7", endColorstr="#0f009d", GradientType=1);
}

a {
  text-decoration: none;
}

.sr-only {
  display: none;
}

/* primary nav  */

.flex {
  display: flex;
  gap: 2rem;
}

.primary-header {
  align-items: center;
  justify-content: space-between;
  background: rgba(0, 0, 0, 0.4);
}

.primary-navigation {
  list-style: none;
  padding: 2rem;
}

.primary-navigation a {
  color: #ffffff;
  text-decoration: none;
  font-size: 1.5rem;
  transition: all 300ms ease-in-out;
  list-style: none;
}

.primary-navigation a:hover {
  color: #929292;
}

.mobile-nav-toggle {
  display: none;
}

@media (max-width: 50rem) {
  .primary-navigation {
    gap: 2rem;
    position: fixed;
    z-index: 1000;
    inset: 0 0 0 0;
    padding: 10rem 3rem;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform 300ms ease-in-out;
    margin: 0;
    backdrop-filter: blur(1rem);
    background: rgba(0, 0, 0, 0.4);
  }

  .primary-navigation a {
    font-size: 1.75rem;
  }

  .primary-navigation[data-visible="true"] {
    transform: translateX(0%);
  }

  .primary-header .mobile-nav-toggle {
    display: block;
    position: absolute;
    z-index: 9999;
    background-image: url("./assets/hamburger.svg");
    background-color: hotpink;
    background-size: cover;
    background-repeat: no-repeat;
    width: 2.5rem;
    border: 0;
    aspect-ratio: 1;
    right: 2rem;
  }

  .primary-header .mobile-nav-toggle[aria-expanded="true"] {
    background-image: url(./assets/close.svg);
    position: fixed;
  }

  .primary-header {
    padding: 4rem;
  }
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: rgba(0, 0, 0, 0.5);
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  list-style: none;
}

.dropdown-content a {
  color: #ffffff;
  padding: 1rem;
  display: flex;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: flex;
  flex-direction: column;
}
<header class="primary-header flex">
      <button class="mobile-nav-toggle" aria-controls="primary-navigation" aria-expanded="false">
        <span class="sr-only">Menu</span>
      </button>
      <nav>
        <ul data-visible="false" class="primary-navigation flex">
          <li><a href="index.html">Home</a></li>

          <li class="dropdown">
            <a href="about.html" class="dropbtn">About Me</a>
            <ul class="dropdown-content">
              <li><a href="projects.html">My previous Projects</a></li>
              <li><a href="quals.html">My Qualifications</a></li>
              <li><a href="cv.html">My CV/Resume</a></li>
              <li><a href="services.html">My Services</a></li>
            </ul>
          </li>
          <li class="dropdown">
            <a href="contact.html" class="dropbtn">Contact Me</a>
            <ul class="dropdown-content">
              <li><a href="social.html">Social Media</a></li>
            </ul>
          </li>
        </ul>
      </nav>
    </header>
6kkfgxo0

6kkfgxo01#

为了消除空白,可以将padding-left: 0添加到.dropdown-content类中。

.dropdown-content {
 // ...
 padding-left: 0;
}
c2e8gylq

c2e8gylq2#

如果我对您的问题理解正确的话,您可能需要对.dropdown-content的样式进行一些调整。
至于你的下拉链接左边的随机空格是由填充引起的。

.dropdown-content {
  display: none;
  position: absolute;

  padding-left: 0; // This will remove the white space on left

  background-color: rgba(0, 0, 0, 0.5);
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  list-style: none;
}

@media (max-width: 50rem) {
// This keeps your dropdown visible in a small screen
  .dropdown-content {
    display: block;
    position: static;
    padding-left: 10px; // This makes it easier to identify which children in the dropdown are linked to the parent.
  }
}

希望这有帮助:)

相关问题