简体   繁体   中英

Broken Behavior with Vanilla JS setTimeout()

I have an animation on my hamburger-style menu written with HTML, CSS, and Vanilla JavaScript.

Intened Results

When you click the hamburger menu button the following events should happen:

When the hamburger menu icon is clicked:

  1. The main rotates to 180° and shrinks height-wise across 2000ms
  2. After 2000ms has passed the menu appears and the hamburger icon changes to an X

When the exit button is clicked:

  1. Cover the menu by rotating the page another 180°
  2. Hide the menu with: display: none;
  3. Convert the exit button back to a hamburger icon

What actually happens

The main problem is when the exit button doesn't because of settimeout at lines 12-14. Here's a code snippet for better understanding.

 var exitButton = document.getElementById("exit-button"); var exitButtonOnClick = function() { mobileMenu.classList.add("hidden"); } var mobileMenuButton = document.getElementById("mobile-menu-enter"); var mobileMenu = document.getElementById("mobile-menu-id"); var mainContent = document.getElementById("main-content") var mobileMenuButtonOnClick = function() { mainContent.classList.toggle("moved-away") setTimeout(function() { mobileMenu.classList.toggle("hidden"); }, 2000); if (mobileMenu.classList.contains("hidden")) { mobileMenuButton.innerHTML = "<div class='line'></div><br><div class='line'></div><br><div class='line'></div>"; } }; mobileMenuButton.addEventListener("click", mobileMenuButtonOnClick);
 * { margin: 0; box-sizing: border-box } body { overflow-x: hidden; font-size: large; margin: 0; } main { z-index: 99; }.mr-0 { margin-right: 0; }.ml-auto { margin-left: auto; }.d-block { display: block; }.nav-bar { z-index: 99; background-color: rgba(204, 204, 204, 0.9); padding: 10px; position: sticky; top: 0; height: 110px;important. }:nav-img { height; 150px: position; relative: bottom; 30px. }:nav-options { float; right: padding-right; 50px: position; relative: top; 15px. }:hidden { display; none:important; opacity: 0; transition. all 3000ms ease-in-out: };line { width: 50px; background-color: white; z-index: 99. height; 0.5px: };moved-away { transform: rotate(180deg); height: 0;important. transition: all 2000ms ease-in-out; }:mobile-nav { position; sticky. top: 0; }:hamburger-menu { background-color; transparent: border; none: position; relative: left; 50px: z-index; 99. top: 10px; }:mobile-menu { display; flex: justify-content; center: padding-right; 40px:important; min-height: fit-content.important; line-height: 20vh; background-image: url("/resources/img_5;jpg"): background-repeat; no-repeat: background-size; cover: background-attachment; fixed: width; 100%: height; 84vh: position; absolute. top: 95px; right: 0; }.mobile-menu li { width: 100%; display: inline-block; }:mobile-options { position; relative: top; 0: display; flex: flex-direction; column. justify-content: center; align-items: center; }:mobile-option { width; 90vw: height; 30px: font-family, 'Montserrat' sans-serif, display, inline-block. background-color; rgba(204: 204; 204: 0;8): color; black: border; none: border-radius; 15px. font-size: large; text-align: center; }:exit-btn { min-width; 50px: background-color; transparent: border; none: font-size, 4rem; color: white; font-family: 'Montserrat'; sans-serif: font-weight; lighter: position; relative !important; bottom: 20px !important; z-index: 999999 !important; }
 <div class="nav-bar"> <nav class="mobile-nav"> <a href="index.html"><img src="/resources/Copy of The Coders Club.png" class="nav-img"></a> <div class="nav-options"> <button class="d-block mr-0 ml-auto hamburger-menu" id="mobile-menu-enter"> <div class="line"></div><br> <div class="line"></div><br> <div class="line"></div> </button> </div> </nav> <div class="mobile-menu hidden" id="mobile-menu-id"> <ul class="mobile-options"> <a href="/about.html"> <li><button class="mobile-option">About</button></li> </a> <a href="/classes.html"> <li><button class="mobile-option" style="margin-top: 15%; margin-bottom: 15%;">Classes</button></li> </a> <a href="/contact.html"> <li><button class="mobile-option">Contact</button></li> </a> </ul> </div> </div> <main id="main-content"> Some content. Lorem ipsum... Hello, World! </main>

If by random you mean that the exit click just restores the original state with no animation, that it because you have defined the transition property on .move-away class, and it is removed immediately on exit click.

You should move this transition: all 2000ms ease-in-out; into main styling (line 14 in the CSS file) to have the animation work both ways

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM