色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

JavaScript中的@keyframes有什么問題

吉茹定2年前9瀏覽0評論

我有這個單頁HTML,我正在用CSS和JavaScript練習一些導航條動畫。我在CSS中編寫了@keyframes,我試圖在JavaScript的事件監聽器中使用它,但它根本不起作用。

const navSlide = () => {

  const burger = document.querySelector('.burger')
  const nav = document.querySelector('.nav-links')
  const navLinks = document.querySelectorAll('.nav-links li')

  burger.addEventListener('click', () => {
    nav.classList.toggle('nav-active')

    navLinks.forEach((link, index) => {
      console.log(link)
      if (link.style.animation) {
        link.style.animation = ''
      } else {
        link.style.animation = `navLinkFade 0.5s ease forward ${index / 7 + 2.5}s`
      }
    })

  })
}
navSlide()

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

nav {
  display: flex;
  justify-content: space-around;
  align-items: center;
  min-height: 8vh;
  font-family: 'Poppins', sans-serif;
  background-color: #5d4954;
}

.logo {
  color: wheat;
  text-transform: uppercase;
  letter-spacing: 5px;
  font-size: 20px;
}

.nav-links {
  display: flex;
  justify-content: space-around;
  width: 30%;
}

.nav-links a {
  color: wheat;
  text-decoration: none;
  letter-spacing: 3px;
  font-weight: bold;
  font-size: 14px;
}

.nav-links li {
  list-style: none;
}

.burger {
  display: none;
  cursor: pointer;
}

.burger div {
  width: 25px;
  height: 2px;
  background-color: wheat;
  margin: 5px;
}

@media screen and (max-width: 1400px) {
  .nav-links {
    width: 60%;
  }
}

@media screen and (max-width: 768px) {
  body {
    overflow-x: hidden;
  }
  .nav-links {
    position: absolute;
    right: 0;
    height: 92vh;
    top: 8vh;
    background-color: #5d4954;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 50%;
    transform: translateX(100%);
    transition: transform 0.5s ease-in;
  }
  .nav-links li {
    opacity: 0;
  }
  .burger {
    display: block;
  }
}

.nav-active {
  transform: translateX(0%);
}

@keyframes navLinkFade {
  from {
    opacity: 0;
    transform: translateX(50px);
  }
  to {
    opacity: 1;
    transform: translateX(0px);
  }
}

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="preconnect" >
  <link rel="preconnect"  crossorigin>
  <link  rel="stylesheet">
  <link rel="stylesheet" href="style.css">
  <title>NAVIGATION</title>
</head>

<body>
  <nav>
    <div class="logo">
      <h4>The Nav</h4>
    </div>
    <ul class="nav-links">
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Work</a></li>
      <li><a href="#">Projects</a></li>
    </ul>
    <div class="burger">
      <div class="line1"></div>
      <div class="line2"></div>
      <div class="line3"></div>
    </div>
  </nav>
  <script src="./script.js"></script>
</body>

</html>

將此添加到CSS

.fade {
      animation-name: navLinkFade;
      animation-duration: 0.5s;
      animation-timing-function: ease;
      animation-fill-mode: forwards;
    }

像這樣使用

navLinks.forEach((link, index) => {
   link.style['animation-delay'] = `${index / 7 + 2.5}s`
   link.classList.toggle('fade')
})