After a page refresh, the jQuery slideDown effect abruptly stops

I've posed this question before without receiving any responses, so I'm giving it another shot.

In WordPress, I crafted a navigation menu that utilizes jQuery for a slide down/up animation on submenus. The issue arises when using flexbox on the submenus, which seems to be causing problems. Changing it to block resolves the issue, but it compromises the desired design aesthetic.

The specific problem and how to reproduce it: Upon initially refreshing the page (or running the code on jsfiddle) and hovering over the menu, the submenu slides down past its intended height and then snaps back into place. Subsequent interactions with other submenus function correctly. However, addressing this snap-back behavior would significantly improve the overall appearance.

Does anyone have insights into why this issue occurs and/or potential solutions?

To test the problem yourself, visit the following jsfiddle link: https://jsfiddle.net/u2zs38oL/

Below are the HTML and CSS codes used in the jsfiddle:

HTML:

<header>
    <nav class="main-navigation" id="desktop-navigation">
        <div class="container-fluid">
            <div class="menu-menu-1-container">
                <ul id="menu-menu-2" class="d-flex justify-content-center">
                    ...
                </ul>
            </div>
        </div>
    </nav>
</header>

CSS:

header .main-navigation {
  display: block;
  margin-left: 1rem!important;
  ...
}

...

jQuery:

jQuery(document).ready(function() {
    
    jQuery('.main-navigation .has-mega-menu').hover(function() {
        ...
    });
});

Answer №1

In my experience, I managed to find a workaround for the issue at hand. By changing the display property of the hidden submenu to flex instead of none upon page reload, it eliminated any problems with snapping. However, starting with the menu open was not ideal.

To address this, I initially set the submenu to flex and concealed the menu using z-index until jQuery loaded. Then, with jQuery, I hid (set display none) the submenu. When hovering over the menu item, I toggled the slideDown effect and increased the z-index for that element.

You can view the updated Fiddle here: https://jsfiddle.net/e319ydkm/

Adjustments I made to the original code:

header .has-mega-menu > .sub-menu {
  display: none -> flex;
  z-index: -1;
}
.hide-under {
  background: #fff;
  z-index: 1;
  height: 1000px;
}

jQuery('.main-navigation .has-mega-menu > .sub-menu').hide();

While this solution is not without its flaws, I believe the issue lies within the use of flexbox (most likely). Given the circumstances, I refrain from marking this as an official answer since it does not provide a definitive resolution.

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

How can you access PHP $_GET values using JavaScript?

Recently, I encountered an issue with my app that is running on localhost/index.php. In my JavaScript code, I am using the History API as follows: history.replaceState({}, null, "?uid=" + uid); to update the URL of the page. Here, the variable uid holds ...

Tips on selecting the active color ID from a list of available color IDs

Currently, I am trying to retrieve the color ID of the active color selection. For example, if I have three colors - yellow, blue, and red - with yellow being the default color. In this scenario, I can obtain the color ID of yellow using a hidden input typ ...

Performing cross-site requests with JQuery AJAX while adhering to CSRF protection in Laravel

Currently, I am developing a web application similar to a CMS using Laravel for the back-end and ReactJS with JQuery for the front-end. I have decided to place the existing Web API on a separate domain called api.test.com, while my user interface is on a ...

When you hover over a single div among a collection of similar ones, reveal the rest

Hey there! I have a bunch of products each with their own description. I'm trying to make the "tile-description" visible when hovering over a "middle tile", and also add a border around the "large-tile" on hover. I've included the markup and some ...

Connect JSON data to a table using knockoutjs

Looking for help on binding my JSON object with knockoutjs. Here's the JSON data: "{\"Sport\":[{\"Name\":\"nana\",\"Description\":\"lmkmsdqd\",\"EndDate\":\"2012-07-22T00:00:00\"} ...

Make text color shift upon clicking

Striving to craft a streamlined about page using JQuery click for altering text and content of the about body without creating separate pages. The concept behind the 'menu' is simple, yet encountering unexpected issues in its functionality. Trig ...

Challenges with placement of Div elements

Check out my jsFiddle example here: http://jsfiddle.net/pqCGd/. Within the jsFiddle, you will find a large messages div that contains multiple message divs. In this example, I have manually provided an example of a message div instead of using auto-genera ...

How long is a 2D array in JavaScript after adding an element?

My 2D array in Javascript always ends up with a length of 0 when I try to push values into it from within a for loop. It remains empty regardless of my attempts. The issue arises because I am unsure about the number of devices that will be stored in mapLi ...

Preventing navbar resizing on websites

I'm trying to have a section on the navbar dedicated for text or alerts that appear based on certain events. However, even when the alert is hidden, it still affects the height of the navbar. How can I resolve this issue? It may be difficult to see i ...

React - updating key prop does not trigger re-render of child component

I have implemented react-infinite-scroll to display a continuous feed of data. My goal is to refresh the data feed whenever the user makes any changes to their settings. To achieve this, I am utilizing a key prop for the InfiniteScroll component. The conc ...

The state of the Toggle Button in Material UI

I am currently working with Material UI's ToggleButton and ToggleButtonGroup components. However, I'm facing an issue where the ToggleButton remains highlighted even after clicking on another button in the group. While the buttons are registering ...

Issue with Web API and AJAX: Unable to retrieve resource, server returned status code 400

[HttpPost("Login")] public async Task<IActionResult> Login([FromBody] SigninViewModel formData) { MemberCredential membercredential = await db.MemberCredential.FirstOrDefaultAsync(t => t.MemberAccount.Equals(f ...

Retrieving information from a server within several sections of a Flask application

Currently working on a project with Python using Flask and Jinja2, I am exploring ways to integrate a sidebar. Within the HTML pages, there is this snippet: {% include "sidebar.html" %} My objective for the sidebar file is to showcase a section highlight ...

Unable to align text within a DIV using CSS

Recently, I started learning jQuery and building my own website. You can find my project on JSFIDDLE. The issue I'm facing is that when hovering over a new div, the text extends beyond the borders of that div instead of staying within it. I've sp ...

Why is my return statement in JavaScript Nextjs mapping values twice?

I am running into an issue where my code is displaying the output twice, and I can't seem to figure out why. Any assistance would be greatly appreciated. Here is a link to the current output that I am receiving. I suspect that the problem lies in sett ...

Preventing distortion of the object when using camera.lookat() in three.js

I have created a piece of code that moves an object randomly on the screen using camera.lookat(). However, I noticed that when the object moves to the sides of the screen, it slightly changes its shape to look at the camera, causing a skewing effect. I wa ...

Leverage the power of JavaScript by combining it with the .on and .editableselect methods

I am facing an issue with a select input that I am trying to make both editable and trigger an ajax request using the on change JS function. However, only one of the functions is working as expected because I have used the same id for the select input twic ...

What is the method to choose the following CSS element?

There are two div elements in my HTML code: <body> <div id="one"></div> <div></div> </body> I am looking to hide the div elements after the one with id="one" using CSS. I attempted this method: #one:after{display: ...

Using jQuery to pass apostrophes from PHP into an input field

Currently, I am working with database information that includes a character of blah^s (I have replaced the ' character with ^ in order to locate apostrophes within the row). I am using preg_replace to add apostrophes back into the string. Everything i ...

Best practices for handling errors beyond network problems when using the fetch() function

I am facing a situation where the route of my fetch() call can result in two different responses, each requiring a different action. However, I have noticed that the catch() method only handles network errors as far as I know. Currently, my code looks lik ...