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

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

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 -->

  1. const navSlide = () =&gt; {
  2. const burger = document.querySelector(&#39;.burger&#39;)
  3. const nav = document.querySelector(&#39;.nav-links&#39;)
  4. const navLinks = document.querySelectorAll(&#39;.nav-links li&#39;)
  5. burger.addEventListener(&#39;click&#39;, () =&gt; {
  6. nav.classList.toggle(&#39;nav-active&#39;)
  7. navLinks.forEach((link, index) =&gt; {
  8. console.log(link)
  9. if (link.style.animation) {
  10. link.style.animation = &#39;&#39;
  11. } else {
  12. link.style.animation = `navLinkFade 0.5s ease forward ${index / 7 + 2.5}s`
  13. }
  14. })
  15. })
  16. }
  17. navSlide()

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

  1. * {
  2. box-sizing: border-box;
  3. padding: 0;
  4. margin: 0;
  5. }
  6. nav {
  7. display: flex;
  8. justify-content: space-around;
  9. align-items: center;
  10. min-height: 8vh;
  11. font-family: &#39;Poppins&#39;, sans-serif;
  12. background-color: #5d4954;
  13. }
  14. .logo {
  15. color: wheat;
  16. text-transform: uppercase;
  17. letter-spacing: 5px;
  18. font-size: 20px;
  19. }
  20. .nav-links {
  21. display: flex;
  22. justify-content: space-around;
  23. width: 30%;
  24. }
  25. .nav-links a {
  26. color: wheat;
  27. text-decoration: none;
  28. letter-spacing: 3px;
  29. font-weight: bold;
  30. font-size: 14px;
  31. }
  32. .nav-links li {
  33. list-style: none;
  34. }
  35. .burger {
  36. display: none;
  37. cursor: pointer;
  38. }
  39. .burger div {
  40. width: 25px;
  41. height: 2px;
  42. background-color: wheat;
  43. margin: 5px;
  44. }
  45. @media screen and (max-width: 1400px) {
  46. .nav-links {
  47. width: 60%;
  48. }
  49. }
  50. @media screen and (max-width: 768px) {
  51. body {
  52. overflow-x: hidden;
  53. }
  54. .nav-links {
  55. position: absolute;
  56. right: 0;
  57. height: 92vh;
  58. top: 8vh;
  59. background-color: #5d4954;
  60. display: flex;
  61. flex-direction: column;
  62. align-items: center;
  63. width: 50%;
  64. transform: translateX(100%);
  65. transition: transform 0.5s ease-in;
  66. }
  67. .nav-links li {
  68. opacity: 0;
  69. }
  70. .burger {
  71. display: block;
  72. }
  73. }
  74. .nav-active {
  75. transform: translateX(0%);
  76. }
  77. @keyframes navLinkFade {
  78. from {
  79. opacity: 0;
  80. transform: translateX(50px);
  81. }
  82. to {
  83. opacity: 1;
  84. transform: translateX(0px);
  85. }
  86. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  7. &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
  8. &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
  9. &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;
  10. &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
  11. &lt;title&gt;NAVIGATION&lt;/title&gt;
  12. &lt;/head&gt;
  13. &lt;body&gt;
  14. &lt;nav&gt;
  15. &lt;div class=&quot;logo&quot;&gt;
  16. &lt;h4&gt;The Nav&lt;/h4&gt;
  17. &lt;/div&gt;
  18. &lt;ul class=&quot;nav-links&quot;&gt;
  19. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
  20. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
  21. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Work&lt;/a&gt;&lt;/li&gt;
  22. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Projects&lt;/a&gt;&lt;/li&gt;
  23. &lt;/ul&gt;
  24. &lt;div class=&quot;burger&quot;&gt;
  25. &lt;div class=&quot;line1&quot;&gt;&lt;/div&gt;
  26. &lt;div class=&quot;line2&quot;&gt;&lt;/div&gt;
  27. &lt;div class=&quot;line3&quot;&gt;&lt;/div&gt;
  28. &lt;/div&gt;
  29. &lt;/nav&gt;
  30. &lt;script src=&quot;./script.js&quot;&gt;&lt;/script&gt;
  31. &lt;/body&gt;
  32. &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中的代码:

  1. .fade {
  2. animation-name: navLinkFade;
  3. animation-duration: 0.5s;
  4. animation-timing-function: ease;
  5. animation-fill-mode: forwards;
  6. }

并且像这样使用:

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

add this to CSS

  1. .fade {
  2. animation-name: navLinkFade;
  3. animation-duration: 0.5s;
  4. animation-timing-function: ease;
  5. animation-fill-mode: forwards;
  6. }

and use like this

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

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:

确定