Issue with jQuery dropdown navigation bar

I am interested in creating a drop-down menu using jquery (here's the corresponding jsFiddle).

HTML:

<ul id="mainmenu">
  <li><a href="http://myszki/">Aktualności</a></li>
  <li class="parent item13"><a href="/start.html">Galerie</a>
    <ul>
      <li><a href="/start/plenery.html">Plenery</a></li>
    </ul>
  </li>
  <li class="parent"><a href="/start.html">Galerie</a>
    <ul>
      <li><a href="/start/plenery.html">Plenery</a></li>
    </ul>
  </li>
</ul>

JS:

$(document).ready(function()
{
     var sub = 'ul#mainmenu li.parent ul';  
     var parents = 'ul#mainmenu li.parent';
     var count = 0;

     $(sub+", "+parents).mouseenter(
     function()
     {         
         $(this).children('ul').addClass('submenu');

         var width = $(parents).width();


         count++;
         $(sub).find('a').css({'width':width}); //setting display parameters

         if ($(sub).is(':visible'))
             {
                 $(sub).stop(true, true).show(); //show
             }
         else
             {
                $(this).find('ul.submenu').stop(true, true).delay(800).slideDown('fast'); 
             }

     }).mouseleave(
         function ()
         {
             count--;

             if (!count)
                 {                     
                     $(sub).stop(true, true).slideUp('fast'); 

                 }
         });

});

CSS:

ul#mainmenu {
    width: 990px; height: 35px;    
    background:#000;
    clear: both;




} 
ul#mainmenu  li {float:left; position: relative;   } 
ul#mainmenu  li a {
    color: #FFFFFF;
    font-size: 14px;
    font-weight: bold;
    text-decoration: none;
    text-transform: uppercase;    
    line-height: 35px; padding: 0 19px 0 20px;
    display: block;

    z-index: 150;
    position: relative;




}
ul#mainmenu  li.backLava {background: url(../images/arrow_menu.png)  no-repeat center bottom #202223; z-index: 20;}
ul#mainmenu  li span {background: url(../images/star.png) left no-repeat; padding-left: 15px; z-index:50;}





ul#mainmenu li.parent ul {display: none; position: absolute; top:35px; }
ul#mainmenu li.parent ul li {border-bottom:1px solid darkgrey; border-left:1px solid darkgrey; border-right:1px solid darkgrey;}
ul#mainmenu li.parent ul span {background: none; padding-left: 4px;}
ul#mainmenu li.parent ul li a {text-decoration: none; background:#eeeeee; color: #000; font-size: 12px; line-height: 25px; display: block; padding: 0px; text-transform: none; opacity:0.8;filter:alpha(opacity=80); font-weight: normal; }
ul#mainmenu li.parent ul li.hover a { color: #000; }

The issue arises when hovering over one parent button causes all parent buttons to expand. What modifications should I make to my code?

Answer №1

Your code seems quite complex. A simplified version could look like this:

$(document).ready(function() {

    $('#mainmenu > li').hover(
        function () {
            $(this).find('ul').stop(true, true).addClass('submenu').slideDown();
        },
        function () {
            $(this).find('ul').stop(true, true).removeClass('submenu').slideUp();
        }
    );

});

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

Trigger an event on an element upon first click, with the condition that the event will only fire again if a different element is clicked

Imagine having 5 span elements, each with an event listener like ` $('.option1').on("click", function(){ option1() }) The event for option 1 is also the default event that occurs on page load. So if no other options are clicked, you won&apo ...

Checking for collisions among numerous elements in JS: An insightful guide

I have created 40 images with random positions, however, some of them collide in certain cases. My question is how to prevent collisions and assign new positions if they do occur? Below is my code along with comments for clarity. $( document ).ready(fun ...

When transmitting data through ajax, escape characters are seamlessly included

I am looking to pass the SQL condition statement using the id of the link element, which represents the category of the product. This information will be processed by a PHP file and then loaded back into the div with the class load_area. To achieve this, I ...

Load the React chunk CSS dynamically to optimize performance and only when it is necessary

After running an audit, Lighthouse is advising me to "Defer unused CSS" for my React app. Despite implementing code splitting and having separate CSS files for each chunk, the suggestion persists. One particular example highlighted by Lighthouse is the foo ...

What is the best way to incorporate JQuery from the node_modules directory instead of using a CDN in a Laravel 5.8 application?

I am facing an issue with integrating JQuery from my npm packages into my Laravel app. In order to make Datatables and other functionalities work, I need JQuery to be properly set up. Although I have installed JQuery using npm, I am encountering an error w ...

Tips for preventing text wrapping in off-canvas design

Seeking advice for my Web Application prototype - looking to prevent text wrapping in off-canvas menu paragraphs using CSS or JS. New to building this type of menu, I used an example from W3Schools to achieve the current design. <!DOCTYPE html> ...

Extract the ID data from the template model form and utilize Django's AJAX to showcase the corresponding information fetched from the database

Currently, my view is set up to display the latest ID and its corresponding data from the database. You can see a sample form by following this link: My goal is to take the displayed ID, send it to my view, increment it, and then show the data related to ...

The div within the button is failing to properly adjust to height settings

Check out this Fiddle I'm currently working on a social thumbs up button and I've encountered some challenges. In my button design, I have included a second div to accommodate the right side of it. However, despite trying to adjust its height us ...

Switching the color of the nav-link in Bootstrap

Struggling to change the color of my Bootstrap's nav-link to white with no success. Here is my code snippet: <nav class="navbar navbar-light" style="background-color: #5E9DFE;"> <a class="navbar-brand" href=& ...

Preserve an array in PHP for future page visits

Imagine I have a webpage named view.php. If I include an argument like view.php?q=fruit, it generates a list of fruits that are stored in an array. What I want to achieve is changing the content of a label on a page to display the content of that array whe ...

Creating a dynamic design with a responsive background image and three columns of engaging content placed on top using Bootstrap

I'm struggling to translate a design mockup into Bootstrap 3.3.6 Here is an image showcasing the concept I am aiming for: https://i.sstatic.net/1jZmh.png The blue background represents an image, with grey text boxes overlaid on top (one of which in ...

Exploring the world of jQuery sliders and dynamically updating div container content

Alright, here's what I have come up with so far: First, take a look at this website to see the idea I am trying to explain: What I am aiming for is this concept: A visitor goes to the provided website and wants to know what type of sandwich they can ...

Text that is superimposed onto an image

Struggling to implement a hovering text over an image? I attempted to follow a tutorial, but couldn't adapt it to my project. While I managed to create a fixed overlay, I aim for a responsive solution that adjusts with resolution changes. My goal is t ...

Conceal the column title for order items in the WooCommerce admin panel

I've been attempting to hide certain columns within the administrative section of Woocommerce, but have had no success with the code I'm using. I'm currently stuck and my question is: Is there a way for me to conceal specific column names i ...

Clicking the save button in the modal updates the text on both the current tab and any previously clicked tabs

I'm looking to implement tabs that, when clicking on the edit icon, open a modal for editing the content of each tab. The issue I'm currently encountering is that if I open the editor for one tab, close it without saving, then open the editor fo ...

Converting Moment JS to Carbon for Time Calculations

How can I synchronize the timestamps between MomentJS and Carbon instances in the GMT timezone? $(document).on('click', '#confirm-reservation .dropdown-menu a', function () { $('#insert_text').text($(this).text()); va ...

Creating a dynamic slideshow with automated arrow navigation is simpler than you may think

I have successfully tested the slideshow and it is functioning perfectly without any issues. I would like to have a dual slideshow setup (slideshow 1 and slideshow 2) with next and previous buttons, but I am interested in adding an automatic sliding featur ...

I'm having trouble getting CSS to apply to my HTML file. Any idea why?

I conducted tests in both Chrome and Firefox, ruling out any browser-related issues. My CSS has been validated and is error-free. However, when I validate my HTML code, an error message pops up stating "Bad value “stylesheet” for attribute rel on eleme ...

Using SetInterval function in conjunction with jQuery's .each() method

I'm looking to cycle through a group of divs and perform random actions at various intervals. I'm trying to use the function below, but the console.log always returns the same object and integer for each iteration. What would be the correct way t ...

Tips for ensuring a custom list item remains at the top when the screen size decreases

My unordered list with custom bullets is displaying some unexpected behavior. When the screen size drops below 364px, the list items wrap to two lines. However, the custom bullet ends up on the second line instead of the first. How can I fix this for smal ...