Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Why does the drawer not animate on the way out?

I want my drawer to animate on the way in, and on the way out and when the animation ends, to turn to display: none but when the drawer is closed, it disappears and doesn't animate out.

const Drawer = ({ closeDrawer, isDrawerOpen }) => {
  const [isAnimating, setIsAnimating] = useState()
  let drawerClassName
  if (isDrawerOpen) {
    drawerClassName = "drawer-in"
  } else if (!isDrawerOpen && isAnimating) {
    drawerClassName = "drawer-animating"
  } else if (!isDrawerOpen && !isAnimating) {
    drawerClassName = "drawer-out"
  }
  return (
    <>
      <div
        className={`drawer ${drawerClassName}`}
        onAnimationStart={() => setIsAnimating(true)}
        onAnimationEnd={() => setIsAnimating(false)}
      ></div>
      <div onClick={closeDrawer}></div>
    </>
  )
}

CSS:

.drawer {
  height: 100%;
  width: 60%;
  background-color: #fff;
  position: absolute;
  right: 0;
  top: 0;
  opacity: 1;
  z-index: 3;
  transform: translateX(100%);
}

.drawer-in {
  animation: 0.7s drawerIn;
  transform: translateX(0);
  display: block;
}

.drawer-animating {
  animation: 0.7s drawerOut;
  display: block;
}

.drawer-out {
  animation: 0.7s drawerOut;
  display: none;
}

@keyframes drawerIn {
  0% {
    transform: translateX(100%);
  }
  1% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(0);
  }
}

@keyframes drawerOut {
  0% {
    transform: translateX(0);
  }
  99% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(100%);
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

it's because when you set the drawer-out class to the element display: none; is activated immediately and you don't see the animation.
One solution is to run a setTimeout function within your JavaScript to wait for the animation to finish, then change the elements display property to none. This will allow your 'closing animation' to complete before removing the element. See my snippet below for working example.

Basically what's happening in my example snippet is I'm triggering the closing animation by adding a class associated with the animation. Then I'm setting a time out function that waits for the animation to complete (set time out in milliseconds to match your animation time within your CSS). Once the timeout is complete, the animation class is removed and the element's data attribute is set to closed which will trigger the display none. Hope this helps.

const menu = document.querySelector('.menu');
const menuToggle = document.querySelector('.menu_toggle');
menuToggle.checked=false

menuToggle.addEventListener('change',(e)=>{
    menuToggle.disabled=true
    let menuState = menu.dataset.menuState
    if(menuState==='closed'){
        menu.dataset.menuState='opened'
        setTimeout(() => {
            menuToggle.disabled=false
        }, 500);
    }else{
        menu.classList.add('animate_close')
        setTimeout(() => {
            menu.classList.remove('animate_close')
            menu.dataset.menuState='closed'
            menuToggle.disabled=false
        }, 500);
    }
})
body {
    background-color: rgb(235, 235, 235);
}

.menu {
    background-color: black;
    color: white;
    width: fit-content;
}
.menu {
    transition: all .5s ease-in-out;
}

.menu[data-menu-state="closed"] {
    background-color: red;
    display: none;
}
.menu[data-menu-state="opened"] {
    animation: openMenu .5s ease-in-out;
    transform: translateX(100%);
    background-color: green;
}
.menu.animate_close{
    background-color: rgb(0, 30, 128);
    animation: closeMenu .5s ease-in-out;
    opacity: 0;
}

@keyframes openMenu {
    0% {
        transform: translateX(0);
    }

    100% {
        transform: translateX(100%);
    }
}

@keyframes closeMenu {
    0% {
        transform: translateX(100%);
    }
    100% {
        transform: translateX(0);
      
    }
}
<body>
    <label for="menu_toggle">Menu Toggle</label>
    <input id="menu_toggle" type="checkbox" class="menu_toggle">
    <div class="menu_container">
        <div class="menu" data-menu-state="closed">
            <ul>
                <li>Item1</li>
                <li>Item2</li>
                <li>Item3</li>
            </ul>
        </div>
        
    </div>
   
    <script src="main.js"></script>
</body>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!