【问题标题】:Toggling CSS Classes with JavaScript Fails to Animate Out使用 JavaScript 切换 CSS 类无法生成动画
【发布时间】:2019-05-03 02:33:54
【问题描述】:

我有一个通过 Javascript 添加一些 CSS 类来触发的移动菜单动画(下面的实时代码 sn-p 中的简化版本)。

我遇到的问题是,虽然菜单动画正确,但当我关闭它时,它只是消失了,并没有像我预期的那样淡出。

我已经在 J​​S 中记录了不同阶段的作用,但我似乎无法让它像 CSS 动画类所说的那样淡出?

任何帮助都会非常棒。

代码笔:https://codepen.io/emilychews/pen/bQOXVm

谢谢,艾米丽。

var mobileMenuButton = document.querySelector(".button")
var mobileMenu = document.querySelector(".menu")

// TOGGLE MOBILE MENU
var clicked = false

function toggleMobileMenu() {
  
  if (clicked === false) {
// removes the `display-none` class added below in the `else` part of the statement, so menu can be toggled more than once
    mobileMenu.classList.remove("display-none") 
    
// changes `display:none` from stylesheet to `display:flex`
    mobileMenu.classList.add("display")                
    
// removes inactive animation that is added below   
    mobileMenu.classList.remove("mobileMenuInactive")
    
// adds the menuActive animation that animates the menu in   
    mobileMenu.classList.add("mobileMenuActive")
    
// changes word on menu button
    mobileMenuButton.textContent = "Close"
    
    clicked = true

  } else {
    
    mobileMenu.classList.remove("mobileMenuActive")
    mobileMenu.classList.add("mobileMenuInactive")
    mobileMenuButton.textContent = "Menu"
    mobileMenu.classList.add("display-none")
    
    clicked = false
  }
}

mobileMenuButton.addEventListener("click", function() {
  toggleMobileMenu()
}, false)
body {
  margin: 0;
  padding: 0;
  display: flex;
  width: 100%;
  height: 100vh;
  justify-content: center;
  align-items: center;
}

.wrapper {
  display: flex;
  align-items: center;
}

.menu {
  display: none;
  align-items: center;
  justify-content: center;
  min-width: 150px;
  background: blue;
  right: 0;
  top: 6rem;
  padding: 10px 0px;
  z-index: 99;
  width: 20%;
  height: calc(100vh - 10rem);
  color: white
}

.button {
  margin: 0 0 0 3rem;
  cursor: pointer;
  padding: 10px 20px;
  background: gray;
  color: white;
}

/*add & remove `display` property*/
.menu.display {display: flex;}
.menu.display-none {display: none;}

/*animations*/
.menu.mobileMenuActive {
  animation: showMobileMenu .5s ease-in forwards;
 }

.menu.mobileMenuInactive {
  animation: removeMobileMenu .5s ease-out forwards;
 }

@keyframes showMobileMenu {
  0% {opacity: 0;}
  100% {opacity: 1;}
}

@keyframes removeMobileMenu {
  0% {opacity: 1;}
  100% {opacity: 0;}
}
<div class="wrapper">
  <div class="menu">Menu</div>
  <div class="button">Click Me</div>
</div>

【问题讨论】:

  • 似乎元素在淡出动画完成之前就被隐藏了。可能必须告诉它等待动画完成,然后再将其更改为 display-none。
  • JS 走最短路线到达终点。在这种情况下,它会忽略不透明度的变化,因为显示被更改为无。
  • 另外another SO question 有一个很好的答案:这个话题
  • 嗨@TimHunter,是的,我明白发生了什么,只是我无法找到可行的解决方案。
  • 嗨@pmkro,您指出的另一个问题/答案与我的问题无关。

标签: javascript html css toggle


【解决方案1】:

您需要等待 500 毫秒(动画的时间)才能将显示传递给 none:

setTimeout(function(){
  mobileMenu.classList.add("display-none");  
},500);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 2022-01-21
  • 2021-10-22
  • 1970-01-01
相关资源
最近更新 更多