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

Breakpoint for mobile menu in FoundationCSS

When the browser is resized, the mobile menu appears at 568x320, which is the default breakpoint. I would like to create a breakpoint around 900px to address the issue with the menu being too large as shown in the image below. However, I am unsure of how ...

The HTML email appears distorted on Yahoo and Outlook, but displays correctly on all other platforms

Basically, I have an HTML email that was converted from a PSD file. I sliced the layers, made some changes to the URLs, and did a test email. The email looks fine on most email clients, but when I send it to Yahoo Mail or Hotmail/Outlook and open it in Chr ...

How to efficiently toggle between multiple classes on a div element using Jquery

I have three distinct divs: If I click on the next or previous div, I would like to remove the blue background from the <h4>. When the <h4> has a blue background (as mentioned above), I want to add a specific class to the <i class="fa fa-l ...

With jQuery fadeout, hyperlinks remain visible even after being clicked on

<a target="_blank" href="https://example.com"> <img class="bcvc_ad1" src="http://bdeas.com/wp-content/uploads/2015/08/ad1.jpg" alt="ad1" width="80" height="80" /> & ...

The foundation grid system is experiencing difficulties when implemented on an Angular form

After successfully installing Foundation 6 on my Angular project, I am facing an issue with the grid system not working properly. Despite numerous attempts to troubleshoot and debug, I have not been able to resolve this issue. If anyone has any insights or ...

Improper Placement of CSS in Google Chrome

I'm facing an issue with my login form that displays perfectly fine in all browsers except Google Chrome. In Chrome, it tends to get distorted more often than not. CSS * { margin: 0; padding: 0; width: auto; } body { background: #E8 ...

Craft a unique parallax effect using Framer Motion's clipping feature

To help visualize my concept, I have sketched it out using Figma. This idea involves a slide transitioning between pages. The goal is to cover the entire page with a sliding effect, and then place a sticker (dubbed the Parallax Box) on top of it. However ...

Can someone show me the steps for reattaching an event to a newly loaded DOM element using ajax?

I am encountering an issue with my click callback function, as it only works once. I suspect this may be due to the fact that parts of my page are being refreshed through ajax actions after the first callback. How can I reattach the event to the newly load ...

Customize the keyboard on your iPod by replacing the default one with a

Looking to customize the iPOD keyboard to only display numbers, similar to a telephone keypad: https://i.stack.imgur.com/X0L3y.png The current iPOD default keyboard looks like this: https://i.stack.imgur.com/VykjU.png ...

The web service that retrieves XML data using jQuery functions perfectly on a local environment, but faces issues when deployed to

I have a webmethod that returns data. I am using jQuery to call this function with its Content type specified as XML. The interesting issue is that it works fine on my local machine but not on the server. Here is the code snippet I am currently using: var ...

Tips for customizing the appearance of date circles and text in Vue V-Calendar.io

On v-calendar.io, there is a demo showcasing the correct display where the date "2018-01-01" has a red background and dark text: However, my display does not match that. I have included Vue.js and v-calendar through CDN, and the page is formatted in HTML ...

What is the best way to eliminate the text-decoration underline from a flex child when the parent is designated as the anchor?

I need help removing the text-decoration of a flex child when the parent is the anchor. Despite trying various CSS code, it doesn't seem to work as expected. On my WordPress site, the underline only shows on hover, but in the jsFiddle example I create ...

Troubleshooting PHP Errors Following an Ajax Response in CodeIgniter Framework

I am currently developing a unique ecommerce website. To experience the issue I'm facing firsthand, you can visit the link below. take a look at my custom ecommerce site » (username: alex, password: superman) The problem I'm encountering is t ...

What is the best way to transform Adobe XD designs into HTML and CSS with Vue.js?

I am looking to create a single page application with vue.js and came across this helpful reference at . A colleague has created a prototype using Adobe XD, and now my task is to transform it into HTML/CSS while making it functional and connecting it to an ...

How to Implement Button Disable Feature in jQuery When No Checkboxes Are Selected

I need help troubleshooting an issue with a disabled button when selecting checkboxes. The multicheck functionality works fine, but if I select all items and then deselect one of them, the button disables again. Can someone assist me with this problem? Be ...

Optimizing CSS usage within Django: top recommendations

Throughout a 6-month-long project, I have continuously added new URLs. However, I am now encountering an issue when using the extend 'base.html' function on other pages where the CSS overlaps and creates confusion. My question is: What are the b ...

CSS clearfix restricted to immediate parent div

Below is the code snippet that illustrates the objective I am attempting to achieve <div> <div style="float:left; width:220px; height:300px; border: 1px solid #aaa"> Left div <br>float:left <br> fixed width 220px </div> ...

Issue: The function view.hlp(...) is not recognized within jsrender

Currently, I am utilizing jsrender to map the templates within a grid. In this process, I have incorporated a method call inside jsrender based on a certain condition as shown below: @section scripts{ $.views.helpers({ isMobile: function () { ...

Implementing class styles using jQuery; however, certain styles fail to be effectively applied

As a beginner, I am experimenting with applying CSS class styles using jQuery. Specifically, I am trying to set two class attributes: color and font size. Surprisingly, only the color attribute is being applied despite both attributes being defined under t ...

saving user information with asynchronous HTTP calls

I am encountering an issue while trying to save my form data using AJAX. When I submit the form data in a JavaScript file that calls another PHP file to perform an insertion operation, an error occurs. Here is the code snippet: <button id="submit" cl ...