css 如何修复我的菜单,使用户在菜单处于活动状态时无法滚动?

thtygnil  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(129)

我想在菜单处于活动状态时使其无法滚动。我已尝试添加一个位置:固定在几个地方,但它会把一切都搞砸。随机元素将加入菜单。我也试图在菜单周围添加一个额外的容器,但这并不起作用。有人知道我如何解决这个问题吗?这真的会帮助我,提前非常感谢你。

// Navbar
const hamburger_menu = document.querySelector(".hamburger-menu");
const nav_container = document.querySelector(".nav-container");

hamburger_menu.addEventListener("click", () => {
  nav_container.classList.toggle("active");
});
/* Nav */

.nav-container {
  min-height: 90vh;
  width: 100%;
  background-image: linear-gradient(135deg, var(--primary-color) 0%, #b8b8b8 74%);
  overflow-x: hidden;
  transform-style: preserve-3d;
  object-fit: cover;
}

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 10;
  height: 3rem;
}

.menu {
  max-width: 72rem;
  width: 100%;
  margin: 0 auto;
  padding: 0 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--white-smoke)
}

.logo {
  font-size: 1.1rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 2px;
  line-height: 4rem;
}

.logo span {
  font-weight: 300;
}

.hamburger-menu {
  height: 4rem;
  width: 3rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.bar {
  width: 1.9rem;
  height: 1.5px;
  border-radius: 25px;
  background-color: var(--white-smoke);
  transition: 0.5s;
  position: relative;
}

.bar:before,
.bar:after {
  content: "";
  position: absolute;
  width: inherit;
  height: inherit;
  background-color: var(--white-smoke);
  transition: 0.5s;
}

.bar:before {
  transform: translateY(-9px);
}

.bar:after {
  transform: translateY(9px);
}

.nav-container .nav-main {
  position: relative;
  width: 100%;
  left: 0;
  z-index: 5;
  overflow: hidden;
  transform-origin: left;
  transform-style: preserve-3d;
  transition: 0.5s;
}

.nav-container.active .bar {
  transform: rotate(360deg);
  background-color: transparent;
}

.nav-container.active .bar:before {
  transform: translateY(0) rotate(45deg);
}

.nav-container.active .bar:after {
  transform: translateY(0) rotate(-45deg);
}

.nav-container.active .nav-main {
  animation: main-animation 0.5s ease;
  cursor: pointer;
  transform: perspective(1200px) rotateY(20deg) translateZ(310px) scale(0.5);
}

@keyframes main-animation {
  from {
    transform: translate(0);
  }
  to {
    transform: perspective(1300px) rotateY(20deg) translateZ(310px) scale(0.5);
  }
}

.nav-links {
  position: absolute;
  width: 30%;
  right: 0;
  top: 0;
  height: 90vh;
  z-index: 2;
  display: flex;
  justify-content: center;
  align-items: center;
}

.nav-links a {
  padding: 0.7rem 0;
  display: inline-block;
  transition: 0.3s;
  opacity: 0;
  transform: translateY(10px);
  animation: hide 0.5s forwards ease;
}

.nav-container.active .nav-links a {
  animation: appear 0.5s forwards ease var(--i);
}

@keyframes appear {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0px);
  }
}

@keyframes hide {
  from {
    opacity: 1;
    transform: translateY(0px);
  }
  to {
    opacity: 0;
    transform: translateY(10px);
  }
}

.shadow {
  position: absolute;
  width: 100%;
  height: 90vh;
  top: 0;
  left: 0;
  transform-style: preserve-3d;
  transform-origin: left;
  transition: 0.5s;
  background-color: rgb(0, 0, 0);
}

.shadow.one {
  z-index: -1;
  opacity: 0.15;
}

.shadow.two {
  z-index: -2;
  opacity: 0.1;
}

.nav-container.active .shadow.one {
  animation: shadow-one 0.6s ease-out;
  transform: perspective(1300px) rotateY(20deg) translateZ(215px) scale(0.5);
}

@keyframes shadow-one {
  0% {
    transform: translate(0);
  }
  5% {
    transform: perspective(1300px) rotateY(20deg) translateZ(310px) scale(0.5);
  }
  100% {
    transform: perspective(1300px) rotateY(20deg) translateZ(215px) scale(0.5);
  }
}

.nav-container.active .shadow.two {
  animation: shadow-two 0.6s ease-out;
  transform: perspective(1300px) rotateY(20deg) translateZ(120px) scale(0.5);
}

@keyframes shadow-two {
  0% {
    transform: translate(0);
  }
  20% {
    transform: perspective(1300px) rotateY(20deg) translateZ(310px) scale(0.5);
  }
  100% {
    transform: perspective(1300px) rotateY(20deg) translateZ(120px) scale(0.5);
  }
}

.nav-container.active .nav-main:hover+.shadow.one {
  transform: perspective(1300px) rotateY(20deg) translateZ(230px) scale(0.5);
}

.nav-container.active .nav-main:hover {
  transform: perspective(1300px) rotateY(20deg) translateZ(340px) scale(0.5);
}
<!-- Nav -->
<div class="nav-container">
  <div class="navbar">
    <div class="menu">
      <a href="index.html">
        <h3 class="logo">Otopoets Galerie</h3>
      </a>
      <div class="hamburger-menu">
        <div class="bar"></div>
      </div>
    </div>
  </div>

  <div class="nav-main-container">
    <div class="nav-main">
      <header>
        <div class="index-header">
          <div class="index-header-content">
            <div class="index-header-text">
              <h1>Neem contact met ons op</h1>
              <p class="small">Passion and experience lead to quality...</p>
            </div>

            <a href="#" class="btn"><span></span>Afspraak inplannen</a>
          </div>
        </div>
      </header>
    </div>

    <div class="shadow one"></div>
    <div class="shadow two"></div>
  </div>

  <div class="nav-links">
    <ul>
      <li>
        <a href="index.html" style="--i: 0.05s;">Home</a>
      </li>
      <li>
        <a href="pakketten.html" style="--i: 0.1s;">Poetspakketten</a>
      </li>
      <li>
        <a href="projecten.html" style="--i: 0.15s;">Projecten</a>
      </li>
      <li>
        <a href="overons.html" style="--i: 0.2s;">Over ons</a>
      </li>
      <li>
        <a href="contact.html" style="--i: 0.25s;">Contact</a>
      </li>
    </ul>
  </div>
</div>

qv7cva1a

qv7cva1a1#

为了防止用户滚动,您可以在事件中的body上切换类no-scroll

const hamburger_menu = document.querySelector(".hamburger-menu");
const nav_container = document.querySelector(".nav-container");
const body = document.querySelector("body");

hamburger_menu.addEventListener("click", () => {
    nav_container.classList.toggle("active");
    body.classList.toggle("no-scroll");
});
.no-scroll {
  overflow: hidden;
}

相关问题