Utilizing a constant variable and the setTimeout function to initiate an animated menu display

I'm currently utilizing jQuery to enhance my dropdown menu functionality by toggling the active class defined in my CSS. However, I've encountered a difficulty in adjusting the timeout for one of the elements. Below is some pseudo code to clarify my issue and simplify it for you all.

PSEUDO CODE

Upon button click, toggle the active class on both the navbar and dropdown menu.
If the dropdown menu has the active class, apply a timeout of .3s and respectively toggle the active class on the content-wrapper.
If the dropdown menu does not have the active class, remove the timeout and perform the toggle action on the content-wrapper.

CODE

//animation loading for the dropdown menu
$("document").ready(function(){
    const navbar = $(".mobile-navbar");
    const contentWrapper = $(".content-wrapper");
    const menuBtn = $(".menu-button-container");
    const dropdown = $(".nav-dropdown-menu");

    menuBtn.click(function(){
        navbar.toggleClass("active");
        dropdown.toggleClass("active");

        // checking if dropdown menu has the active class
        // setting a timeout of .3 seconds for the content
        // wrapper accordingly

        if(dropdown).hasClass("active"){
            setTimeout(function(){
                contentwrapper.toggleClass("active");
            }, 300);
        }
        else {
            setTimeout(function(){
                contentWrapper.toggleClass("active");
            }, 0);
        }
     });
});

Answer №1

Let's see if I can assist you with your current issue.

It's unclear why a timeout is necessary in this scenario. In the problem description provided, it mentions removing the timeout but not utilizing the clearTimeout() function.

Below is a revised approach to consider:

// Implement animation for dropdown menu
$("document").ready(function(){
 const $navbar = $(".mobile-navbar");
 const $contentWrapper = $(".content-wrapper");
 const $menuBtn = $(".menu-button-container");
 const $dropdown = $(".nav-dropdown-menu");
 let myTimeout;

 $menuBtn.click(function(){
   $navbar.toggleClass("active");
   $dropdown.toggleClass("active");

   if ( $dropdown.hasClass("active") ) {
     myTimeout = setTimeout(function(){
       $contentwrapper.toggleClass("active");
     }, 300);
   }
   else {
      clearTimeout(myTimeout);
      $contentWrapper.toggleClass("active");
   }
 });
});

I've updated the naming of your constants by prefixing them with $. It's a common practice for jQuery objects. Keep up this good habit!

I hope this solution proves helpful.

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

Is it possible to transfer an HTML table to a PowerPoint presentation directly from the client

Is there a specific jquery or javascript solution available for converting an HTML table directly into a PowerPoint presentation? So far, the only solution I have come across is html table export, which provides export options for various file formats. H ...

Designing visual representations using the Canvas elementorGenerating graphic content

I want to design a creative text image where multiple characters come together to form a word, like "The Matrix", within an animated canvas with a matrix effect. The challenge I'm facing is figuring out how to display the text inside the canvas and th ...

Populate the ListBox based on the value of the input control in the onKeyUp event

I have a ListBox control <asp:ListBox ID="ListBox1" runat="server"> <asp:ListItem Value="1" Text="XYZ" /> <asp:ListItem Value="0" Text="XYZD" /> <as ...

Changing the order of element names depending on their location within the parent element using jQuery

<div class="content"> <div> <input type="text" name="newname[name]0"/> <div> <input type="text" name="newname[utility]0"/> <div> <textarea name="newname[text]0 ...

Having trouble accessing a website that is embedded within an iframe, and experiencing issues with several ajax post requests not functioning properly within the iframe specifically on Chrome

I am currently dealing with a situation where I have two distinct websites, (constructed with PHP) and (also a PHP site). To integrate them, I have embedded site1 inside site2 using an iframe. <iframe src="https://site1.com" seamless width= ...

Is there a way to prioritize the table row background color over the cell input elements background color?

After a user selects a table row, the row is assigned the class below: tr.selected { background-color:#FFA76C; } However, if there are input fields in the cells with background colors, they end up overriding the row's background color. (For instance ...

The navbar does not dictate the views

Greetings lovely individuals I've been grappling with this issue for a while now and it seems I can't quite crack it on my own... I'm hopeful that you can assist me. My objective: A functioning navigation bar controlled by PHP in an MVC pat ...

The hover drop-down menu in CSS is not appearing in the correct position

Having trouble with my navigation bar - the dropdown menu isn't showing up below the category when I hover over it. It keeps ending up in the left-hand corner and I can't access it! I've searched everywhere for a solution, but can't fin ...

jquery slider for inputting phone number on a mobile device

I am currently working on enhancing the user interface by implementing a feature where users can select a mobile number using a slider. The idea is that the user will choose a digit between 0 and 9 using the slider, then confirm their selection by pressing ...

Tips on concealing and deactivating the fullscreen option in flowplayer

Could someone please assist me in resolving this issue? I've been attempting to hide the fullscreen button from the flowplayer, but so far, I haven't found a solution. Below is my JavaScript code: <script> $f("player", "flowplayer/flowpla ...

Ways to retrieve JSON information and incorporate it into an if statement in this specific scenario

Here is the AJAX function code I have written: $('#request_form').submit(function(e) { var form = $(this); var formdata = false; if (window.FormData) { formdata = new FormData(form[0]); } var formAction = form.attr( ...

The fonts in node.js are not functioning as expected, without displaying any error messages

I'm having trouble implementing custom fonts on my website. Despite following the correct file structure, the fonts do not seem to be loading. My project files are organized in the following manner: https://gyazo.com/5ee766f030290e5b2fa42320cc39f10b ...

Should I utilize a single form or one for each table row in asp.net mvc?

While working on a project, I caught myself in a coding dilemma: <table> <tr> <th>Code</th> <th>Enabled</th> <th>Percentage</th> <th>Amount</th> <th>Start&l ...

Occasionally, PHP mail will send an email with just the subject line and an empty

Occasionally, I encounter an issue with the PHP mail form on my website where it sends only the subject line and leaves the body empty. This problem seems to occur randomly and rarely. Initially, I assumed that users were submitting the form too early resu ...

The vertical shift in one of the inline table cell DIVs is being caused by two of them

I've been searching for a solution to my issue, but have come up empty-handed. I apologize if this has already been discussed before. My HTML page contains a section that appears as follows: <div id=wrap> <div id=lb> Content ...

Ensure that the left div spans 100% of the width of the container, while the right div remains on the right side and maintains a

Is there a way to create an effect with a div where the left side is 100% and the right side stays fixed? <!DOCTYPE html> <html> <style> .left { width: 100%; float: left; border: #ccc solid 1px; } .right { width: 50px; float:left; border ...

Is it important that the end date does not exceed the start date?

In my request, the end date cannot be later than the start date. For example, if the start date is 24/4/2017 and the end date is 23/4/2017, the search function should be disabled for the calendar date 23/4/2017. Only dates from 24/4/2017 onwards should be ...

Utilizing Laravel's URL::asset method in conjunction with a JavaScript variable

Having a go at creating an HTML tag using the Jquery snippet below $("<option />",{ 'data-src':"{{ asset(my-javascript-variable) }}", id:'my_id').appendTo($('#image')); An option tag is being added to a select element. ...

Is there a way to postpone the mouseover event for vertical tabs?

While this may seem like a simple question to some, my programming skills are not quite up to par. I am seeking help from someone who can assist me. My goal is to delay the mouseover event on vertical tabs so that users can jump directly from tab 1 to ta ...

Unable to achieve text centering within a button using HTML and CSS

I'm diving into the world of HTML/CSS for the first time and encountering some issues along the way. In my setup.html, I created a "Continue" button and applied a class to style it in styles.css. However, I'm facing an issue where text-align cen ...