javascript - 切换 CSS 动画

标签 javascript css

我正在尝试创建一个动画,它在单击时从左侧的导航栏中滑动,然后在再次单击时反转动画。我遇到的唯一问题是反向部分,它跳过了整个动画但消失了。我不想使用 JQuery - 我正试图在这个元素中远离它。

let body = document.querySelector("body")
let navigation = document.getElementById("menu");
let mobileNav = document.getElementById("mobile-navigation-container");
let mobileClose = document.getElementById("mobile-close")

navigation.addEventListener("click", function(){
   mobileNav.classList.remove("animation-navEnd");
   mobileNav.className = "animation-navStart"
   body.className += "stop-scrolling"
})

mobileClose.addEventListener("click", function(){
   mobileNav.className = "animation-navEnd";
   mobileNav.classList.remove("animation-navStart");
   body.classList.remove("stop-scrolling");
})
#mobile-navigation-container {
    height: 100%;
    background-color: #687b7f;
    z-index: 100;
    position: absolute;
    width: 100%;
    left: -100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
  
#mobile-close {
  display: flex;
  height: 10vh;
  width: 80%;
  justify-content: flex-end;
  align-items: center;
  font-size: 35px;
  color: #000000;
}

#mobile-navigation {
  display: flex;
  height: 90vh;
  width: 80%;
}

#mobile-navigation ul li {
  font-size: 28px;
  margin-top: 15px;
  font-family: "Roboto" !important;
}

#navbar-container {
  width: 100%;
  position: absolute;
  display: flex;
  align-items: center; 
  justify-content: center;
  color: #000000;
  z-index: 5;
}

#navigation {
  width: 350px;
  display: flex;
  justify-content: flex-end;
}


.animation-navStart{
  animation: navStart 0.5s forwards;
}

.animation-navEnd{
  animation: navEnd 0.5s forwards;
}

@keyframes navStart {
  100%{ left : 0;}
}

@keyframes navEnd {
  100%{ left : -100%;}
}
<div id="mobile-navigation-container">
    <div id="mobile-close">
        Close
    </div>
    <div id="mobile-navigation">
        <ul>
            <li><a href="/home">Home</a></li>
            <li><a href="/about">About</a></li>
            <li><a href="/services">Services</a></li>
            <li><a href="/contact">Contact</a></li>
            <li><a href="/blog-posts">Blog</a></li>
        </ul>
    </div>
</div>

<div id="navbar-container">
    <div id="navigation">
       <div id="menu">Menu</div>
    </div>
</div>

最佳答案

尝试给你的 css 动画一个起点,并确保尝试使用相同的“单位”,例如 %、px 等

let body = document.querySelector("body")
let navigation = document.getElementById("menu");
let mobileNav = document.getElementById("mobile-navigation-container");
let mobileClose = document.getElementById("mobile-close")

navigation.addEventListener("click", function(){
  mobileNav.classList.remove("animation-navEnd");
  mobileNav.className = "animation-navStart"
  body.className += "stop-scrolling"
})

mobileClose.addEventListener("click", function(){
  mobileNav.className = "animation-navEnd";
  mobileNav.classList.remove("animation-navStart");
  body.classList.remove("stop-scrolling");
})
#mobile-navigation-container {
    height: 100%;
    background-color: #687b7f;
    z-index: 100;
    position: absolute;
    width: 100%;
    left: -100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
  
#mobile-close {
  display: flex;
  height: 10vh;
  width: 80%;
  justify-content: flex-end;
  align-items: center;
  font-size: 35px;
  color: #000000;
}

#mobile-navigation {
  display: flex;
  height: 90vh;
  width: 80%;
}

#mobile-navigation ul li {
  font-size: 28px;
  margin-top: 15px;
  font-family: "Roboto" !important;
}

#navbar-container {
  width: 100%;
  position: absolute;
  display: flex;
  align-items: center; 
  justify-content: center;
  color: #000000;
  z-index: 5;
}

#navigation {
  width: 350px;
  display: flex;
  justify-content: flex-end;
}


.animation-navStart{
  animation: navStart 0.5s forwards;
}

.animation-navEnd{
  animation: navEnd 0.5s forwards;
}

@keyframes navStart {
  0% {
   left: -100%;
  }
  100%{ 
   left: 0%;
  }
}

@keyframes navEnd {
  0% {
   left: 0%;
  }
  100%{ 
   left: -100%;
  }
}
<div id="mobile-navigation-container">
  <div id="mobile-close">Close</div>
  <div id="mobile-navigation">
    <ul>
      <li><a href="/home">Home</a></li>
      <li><a href="/about">About</a></li>
      <li><a href="/services">Services</a></li>
      <li><a href="/contact">Contact</a></li>
      <li><a href="/blog-posts">Blog</a></li>
    </ul>
  </div>
</div>

<div id="navbar-container">
  <div id="navigation">
     <div id="menu">Menu</div>
  </div>
</div>

关于javascript - 切换 CSS 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58294121/

相关文章:

JavaScript 本地时间打印

javascript - Google maps api 在 firefox 上不断加载

javascript - 阻止网页页面跳转到顶部

html - 从多个 HTML、CSS 中选择一个输入

javascript - Bootstrap 3 - 如何使用 javascript/jquery 更改显示为工具提示的超链接标题文本?

javascript - 将工具提示添加到 highcharts javascript

javascript - 混淆/缩小动态生成的 javascript 的最佳方法是什么

html - Twitter Bootstrap 3 - 如何根据屏幕尺寸从水平按钮组切换到垂直按钮组?

jquery - 可排序不适用于不同高度的列

javascript - jQuery 日期选择器上的 afterShow 事件