Tips for reducing the delay between pressing a button and starting an animation

Currently working on a hamburger menu with animations for displaying and hiding the menu after clicking a button. I'm facing an issue where there is a delay in hiding the menu equal to the animation time. Despite trying multiple solutions, the delay persists.

@keyframes appear {
    from {
        max-height: 0;
    }
    to {
        max-height: 100vh;
    }
}
@keyframes hide {
    from {
        max-height: 100vh;
    }
    to {
        max-height: 0;
    }
}

.header__menu{
    position: absolute;
    left: 0;
    top: 2.6em;
    flex-direction: column;
    width: 100vw;
    color: #fff;
    text-transform: lowercase;
    @include concert-one;
    font-size: 1.8rem;
    background-color: $violet;
    text-align: center;
    overflow: hidden;
    display: none;
}

and JS

hamburgerMenu.style.display = "none";

hamburgerButton.addEventListener("click", () => {
    if (hamburgerMenu.style.display == "none") {
        hamburgerMenu.style.animation = "appear 1s ease-in both";
        hamburgerMenu.style.display = "flex"
    } else if (hamburgerMenu.style.display == "flex") {
        hamburgerMenu.style.animation = "hide 1s ease-in both";
        setTimeout(() => {
            hamburgerMenu.style.display = "none";
        }, 1000);
    } 
})

HTML

    <header class="header">
        <div class="header__logo">
            travel
        </div>
        <nav class="header__nav">
            <a href="#"><img src="public/img/icons/whatsapp.svg" alt="Whatsapp"></a>
            <a href="#"><img src="public/img/icons/twitter.svg" alt="Twitter"></a>
            <a href="#"><img src="public/img/icons/facebook.svg" alt="Facebook"></a>
            <a href="#"><img src="public/img/icons/instagram.svg" alt="instagram"></a>
        </nav>
        <div class="header__hamburger">
            <span></span>
            <span></span>
            <span></span>
        </div>
        <div class="header__menu">
            <span>Whatsapp</span>
            <span>Twitter</span>
            <span>Facebook</span>
            <span>Instagram</span>
        </div>
    </header>

Answer №1

It appears that the setTimeout function is currently set for 1 second, but it can be optimized to run faster by reducing the timeframe to a fraction of a second.

Consider implementing the following code snippet:

hamburgerMenu.style.display = "none";

hamburgerButton.addEventListener("click", () => {
    if (hamburgerMenu.style.display == "none") {
        hamburgerMenu.style.animation = "appear 1s ease-in both";
        hamburgerMenu.style.display = "flex"
    } else if (hamburgerMenu.style.display == "flex") {
        hamburgerMenu.style.animation = "hide 1s ease-in both";
        setTimeout(() => {
            hamburgerMenu.style.display = "none";
        }, 100);
    } 
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am having trouble calling a method in the subscribe function because it is showing an error message saying "Cannot read property 'showMessageFromSocket' of undefined"

My goal is to call a method within the subscribe block, but I am encountering an error that says "Cannot read property 'showMessageFromSocket' of undefined". How can I successfully call the showMessageFromSocket method? In Angular, this was possi ...

Unable to hide the mobile menu button

https://i.sstatic.net/5rdYY.pngI am currently working on a fun website project . I am facing an issue with the mobile menu button not disappearing using display:none in Safari on my iPhone when in landscape mode, even though it works fine in Chrome. My g ...

Overcoming Tabindex Issues with jQuery

Currently, I am working on managing the user interaction with my forms using the tabindex property of form elements. I have specified the tabindex for all elements and I want to ensure that this value is respected and utilized correctly. While developing ...

Setting up Visual Studio Code for debugging HTML and JavaScript code

Struggling to troubleshoot some HTML/JavaScript using VSCode. After installing the Chrome debugger extension and configuring the launch.json as follows: { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of ex ...

Identify input elements that specifically contain an onclick event

Several of the input elements on my page have a function called setSomeFunction() that either shows or hides certain divs when clicked. I know I can locate all the input elements using document.getElementsByTagName("input") and store them in an array. How ...

Having trouble getting the JSONP request to render properly, encountering persistent errors

Need assistance with JSONP Rendering to HTML. The request is present in the source code, but I keep encountering errors on the foreach line. Any help would be greatly appreciated. <meta name="viewport" content="width=device-width, initial-scale=1"&g ...

Encountering a 500 server error when making an Ajax call to a SOAP web method in the .Net framework

Having trouble with two different async calls using ajax. Both are done the same way, but one has parameters for the web method. The link is correct, as I get the return correctly in the browser when I follow it, but I keep getting a server 500 error. Any ...

Uncovering the Secret to Extracting a JSON Array from a JSON Data Structure Using JS, REST, and NodeJS

Within my Python code, I am working with a JSON object that contains various fields, including one labeled field3 which holds a list of additional JSON objects. This JSON object is sent from a Python script to a REST service running on JS with the Express ...

Leveraging dual style.bind declarations in Aurelia

I'm facing an issue with utilizing two style.bind in Aurelia as it doesn't seem to be functioning properly. Alternatively, I could attempt using just one style.bind and applying the desired styles, but I am unsure of how to proceed. Here is the ...

I'm facing a challenge with retrieving the controller's scope in my Angular directive

Having trouble accessing the settings I receive from the server in my app. The directives are set up correctly and the $http is used to fetch a js file with the app settings. However, despite being able to log the scope in the console, I am unable to acces ...

Retrieving data attributes from model within a Backbone view using Underscore template

I have a Backbone.js view that I'm working with: var StreamV = Backbone.View.extend({ tagName: 'div', className: 'stream', events: { }, initialize: function () { this.listenTo(this.model, 'change' ...

Element does not cross both explicit and implicit columns

When working in a grid container with only 1 column and 1 row, if there is an implicit column in the first row, how can an element in the second row (as shown by the green column in the example) span across both the explicit and implicit columns? I appreci ...

Adjusting SVG Size for Mobile Devices

How do I make my SVG Logo responsive on mobile devices? Check out my Fiddle and find the code below: body { background:black; } @media screen and (max-width: 500px) { svg { width:50%; } } <?xml version="1.0" encoding="UTF-8" sta ...

The fadeOut method does not support the removeClass function

I've encountered a small issue while trying to create a slider with images. As someone who is new to javascript and jquery, I attempted to build it based on my past experience with Codeacademy. However, the code that used to work fine there is now cau ...

Manipulating objects with Jade Mixin

My current challenge involves programmatically displaying objects within a jade template by passing an array of objects to the view. The aim is to present these objects in a grid view with up to 3 items per row. However, I encountered an issue where nested ...

In both Chrome and Edge, the default value for the <select> tag is successfully set, however, this functionality is not working in

I have defined two values in the created method, 2018 and My Name, and assigned them to separate data properties. These data properties are then passed as v-bind to a component. The issue I am facing is that in Chrome and Edge, both values are set as defa ...

Utilize JavaScript to connect WhatsApp with the website

I am looking to use Javascript to enable opening WhatsApp on both iOS and Android devices. Below is the code I have attempted: window.open("https://wa.me/15551234567"); window.open("https://api.whatsapp.com/send?phone=15551234567"); ...

Failure to load a picture does not trigger onError in the onLoad function

Is there a way to ensure that an image always loads, even if the initial load fails? I have tried using the following code snippet: <img class="small" src="VTVFile4.jpg" onload="this.onload=null; this.src='VTVFile4.jpg?' + new Date();" alt=" ...

What is the process of utilizing jQuery to hover over a relevant cell?

I'm interested in learning how to implement jQuery to highlight related tables. Initially, I explored this JS fiddle and discovered a method for highlighting both vertically and horizontally. I conducted several searches to find a similar approach, ...

Create a token for

Visit this link for more information on listing functions in Azure web apps https://i.sstatic.net/YgsF5.png Is there a way to dynamically generate the green-highlighted token using JavaScript or an API? While I'm aware that the token can be generate ...