这是JavaScript中的@keyframes有什么问题?

huangapple go评论134阅读模式
英文:

What is wrong with this @keyframes thing in JavaScript

问题

I have this single page HTML and I'm practicing some navBar animation with CSS and JavaScript. I wrote @keyframes in CSS and I'm trying to use it in an event listener in JavaScript but it doesn't work at all.

英文:

I have this single page HTML and I'm practicing some navBar animation with CSS and JavaScript. I wrote @keyframes in CSS and I'm trying to use it in a event listener in JavaScript but it doesn't work at all.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const navSlide = () =&gt; {

  const burger = document.querySelector(&#39;.burger&#39;)
  const nav = document.querySelector(&#39;.nav-links&#39;)
  const navLinks = document.querySelectorAll(&#39;.nav-links li&#39;)

  burger.addEventListener(&#39;click&#39;, () =&gt; {
    nav.classList.toggle(&#39;nav-active&#39;)

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

  })
}
navSlide()

<!-- language: lang-css -->

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

nav {
  display: flex;
  justify-content: space-around;
  align-items: center;
  min-height: 8vh;
  font-family: &#39;Poppins&#39;, 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);
  }
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
  &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
  &lt;link href=&quot;https://fonts.googleapis.com/css2?family=Poppins:wght@200;400;500;700&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
  &lt;title&gt;NAVIGATION&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;nav&gt;
    &lt;div class=&quot;logo&quot;&gt;
      &lt;h4&gt;The Nav&lt;/h4&gt;
    &lt;/div&gt;
    &lt;ul class=&quot;nav-links&quot;&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Work&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Projects&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;div class=&quot;burger&quot;&gt;
      &lt;div class=&quot;line1&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;line2&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;line3&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/nav&gt;
  &lt;script src=&quot;./script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

Sorry! I'm too new here and I don't know how to beautify my text

wrote @keyframes in CSS, won't work in JavaScript

答案1

得分: 0

将这部分内容翻译成中文如下:

添加到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')
})
英文:

add this to CSS

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

and use like this

navLinks.forEach((link, index) =&gt; {
   link.style[&#39;animation-delay&#39;] = `${index / 7 + 2.5}s`
   link.classList.toggle(&#39;fade&#39;)
})

huangapple
  • 本文由 发表于 2023年5月11日 07:27:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定