Keep the nested ul with absolute positioning open until the pointer is moved outside the element

I am working on a navigation menu where the last list item (li) contains a nested ul for a dropdown menu. My goal is to display this nested menu when hovering over the parent li and hide it when moving away from the menu. However, I am running into an issue where the menu glitches out when trying to mouse into it, possibly due to the absolute positioning of the sub-menu. How can I fix this problem?

$(function() {
  $('li.has-sub')
  .mouseover(function() {
$(this).find('ul').slideDown();
})
.mouseout(function() {
$(this).find('ul').slideUp();
});
});
ul {
  list-style-type:none;
  padding:0 30px;
}
ul.nav {
  border:1px solid black;
}
ul.nav > li {
  display:inline-block;
  line-height:50px;
  margin-left:30px;
}
  ul.nav > li:first-child {
    margin-left:0;
  }
  ul.nav > li > ul {
    display:none;
    position:absolute;
    border:1px solid black;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav">
  <li>Link One</li>
  <li>Link Two</li>
  <li>Link Three</li>
  <li class="has-sub">
    Link Four
    <ul>
      <li>Sub Link One</li>
      <li>Sub Link Two</li>
      <li>Sub Link Three</li>
    </ul>
  </li>
</ul>

Answer №1

Don't forget to include the .stop() method in your code:

$(document).ready(function() {
  $('li.has-sub').mouseover(function() {
    $(this).find('ul').stop().slideDown();
  })
  .mouseout(function() {
    $(this).find('ul').stop().slideUp();
  });
});
ul {
  list-style-type:none;
  padding:0 20px;
}

ul.nav {
  border:1px solid #333;
}

ul.nav > li {
  display:inline-block;
  line-height:40px;
  margin-left:20px;
}

ul.nav > li:first-child {
  margin-left:0;
}

ul.nav > li > ul {
  display:none;
  position:absolute;
  border:1px solid #333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<ul class="nav">
  <li>Item One</li>
  <li>Item Two</li>
  <li>Item Three</li>
  <li class="has-sub">
    Item Four
    <ul>
      <li>Subitem One</li>
      <li>Subitem Two</li>
      <li>Subitem Three</li>
    </ul>
  </li>
</ul>

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

Place picture in the bottom right corner of the div without using absolute positioning

I am looking for a way to position an image in the lower right corner of a div, but I want to avoid using absolute positioning. The reason for this is that when I use absolute positioning, the text wrap is affected due to the float right applied to the ima ...

utilize jQuery to load webpage with an HTML dropdown element

Querying the Campaigns: // Getting the campaigns $campaigns = $wpdb->get_results( "SELECT * FROM tbl_campaigns ORDER BY campaignID DESC", OBJECT_K ); // Displaying the Cam ...

What is the proper way to assign a class name to an animation state in Angular 2/4?

I am currently working with Angular Animations using version 4.1.3 Check out the code snippet below: @Component({ selector : 'my-fader', animations: [ trigger('visibilityChanged', [ state('true' , style({ opaci ...

Error: The JQUERY autocomplete is throwing an uncaught type error because it cannot read the property 'length' of an undefined value

These scripts are being utilized at this source I have implemented jQuery Autocomplete to search for users in my database. Below is the controller code returning Json: public function searchusers1() { if ($_GET) { $query = $this -> input ...

Having trouble triggering a click event with JQuery

I have a hidden link for downloading Audio Files that I want the user to access using a button. However, I am having trouble triggering the click event. Below is the HTML code: <a class="APopupDown" data-icon="home" id="DownloadFile" href="http://yaho ...

What to do when CSS Overflow doesn't work as expected?

Are there any alternatives for browsers that don't support CSS overflow? I am working on a new layout that heavily relies on the overflow property, however it seems like android browsers do not handle it well and as a result, my layout is broken. I am ...

Unable to insert flag image into the <option> tag

Struggling to add a flag image to the options using any method (html,css)! <select> <option value="rus" selected>Rus</option> <option value="Uzb" >Uzb</option> <option value="eng">Eng</option> </s ...

Using PHP to send multiple data through ajax

Currently, I am facing an issue with jQuery where I am unable to pass more than one selected data from "Forms.html" to be sent to "ajaxServer.php" using jQuery "ajax.js". The problem arises when trying to receive multiple values at "ajaxServer.php", as one ...

React Intersection Observer not functioning properly

Hey there! I'm trying to create an animation where the title slides down and the left element slides to the right when scrolling, using the intersection observer. Everything seems to be fine in my code, but for some reason it's not working. Any t ...

The Array.push method is part of the Array class, while the $.inArray function returns the value "false"

I have two sets of elements and I am looking to combine the values from the first set with the second one. My approach involved using the .push method. Here is how I initialized the arrays: <script> var primarySet = ["Apple", "Banana"]; var seconda ...

Utilizing grease/tampermonkey (or any other browser extension) to filter out specific characters within webpage elements

Forgive me if my language is not accurate, as I am still learning about programming. The forum that I visit has cluttered the page with unnecessary and glitchy images and text for a promotion. This has made the forum difficult to navigate. I was successful ...

Unable to trigger AJAX Complete Handler

Upon completing extensive backend work on my web application, I discovered that the GetMeasure Request was taking up to 10 seconds to finalize. To prevent confusion for potential users, I decided to implement an overlay so that they would not be left stari ...

What is the best way to ensure the content div fills all available space? (JQM compatibility concerns)

Currently, I am facing an issue with displaying a pdf in an iFrame within my app. The problem lies in the fact that the iFrame does not occupy the entire space between the header and footer divs on the page. I am utilizing Jquery Mobile 1.1.0 for the theme ...

What is the best method for submitting a form via ajax that has already been loaded using ajax, all without needing to refresh the current

I have been struggling with a problem for almost a week now. I need to submit a form using ajax, which was already loaded with ajax. I have tried multiple solutions but nothing seems to work. If anyone knows the right approach, I would greatly appreciate y ...

Creating a popup window for multiple file uploads in ASP.NET using VB.NET

Hello, I am trying to create a popup window with multiple file upload options. When the 'UploadDocument' button is clicked, I want the popup window to appear. I have attempted to do this but it is not working as expected. Currently, the popup ap ...

Utilize the Zend Framework to Customize the jQuery Date Picker for Localized Date Selection

I've been attempting to customize a datepicker with a locale other than English. I followed the instructions in the jQuery manual which suggested adding this line of code: $("#datepicker").datepicker($.datepicker.regional['fr']); to $view- ...

Design a Hover Mega Menu - click outside to close

Although there are similar topics on stackoverflow, the code I have is different from them. I am trying to achieve the following: If I click on the search box (the white square on the site), my search box should open If I open the search box and then cl ...

Incorporating promises with ajax to enhance functionality in change events

Consider the scenario where you trigger an ajax request upon a change event in the following manner: MyClass.prototype.bindChangeEvent = function(){ $(document).on('change', '#elementid', function(){ var $element = $(this); $ ...

What method can be used to eliminate the shadow of a container when rotating a div?

I am currently working on creating a dynamic card that expands and reveals additional information upon mouse rollover. The challenge I am facing is that the shadow effect remains visible when flipping the card, which disrupts the overall desired effect. H ...

Is it possible to drag and drop without using jQuery UI?

Currently, I'm developing a jquery plugin that incorporates drag and drop functionalities using HTML5 drag attributes. The functionality is working well, except for one issue - linking the function names to their designated targets. Although the func ...