Menu with customized hover feature, revealing submenu upon mouse movement

I have implemented my own hover event handler to display a triangle pointer and a horizontally aligned submenu bar. It works well for showing and navigating to the submenu when the top menu item is clicked. However, I am facing an issue where the submenu bar disappears when attempting to move the mouse to the submenu.

TIA

screenshot https://i.sstatic.net/TlOJU.png

Code:

$("#menubar li.dropdown>a.nav-link").hover(function() {
    if ($("#menubar li.dropdown").filter(function() {
        return $(this).data("show")
      }).length === 0)
      $(this).closest("li").addClass("active show");
  },
  function() {
  /*when top menu is clicked, don't hide submenu*/
    if ($("#menubar li.dropdown").filter(function() {
        return $(this).data("show")
      }).length === 0)
      $(this).closest("li").removeClass("active show");
  });

$("#menubar li>a.nav-link").on("click", function(e) {
  e.stopPropagation();

  var $li = $(this).closest("li");

  if (!$li.hasClass("dropdown")) {
    $("#menubar li.dropdown").removeClass("active show");
    $("#menubar li.dropdown").data("show", false);
  } else {
    if ($li.data("show")) {
      $li.removeClass("active show");
      $li.data("show", false);
    } else {
      $("#menubar li.dropdown").removeClass("active show");
      $li.addClass("active show");
      $li.data("show", true);
    }
  }
});
$("#menubar ul.dropdown-menu>li>a").on("click", function(e) {
  $("#menubar li.dropdown").removeClass("active show");
  $("#menubar li.dropdown").data("show", false);
});
#menubar ul {
  height: 44px;
  padding-top: 5px;
}

#menubar .nav-link {
  font-size: 14px;
  padding: 8px 50px 0 0 !important;
  color: white !important;
}

#menubar .nav-link:focus,
#menubar .nav-link:hover,
#menubar .nav-link:visited {
  color: white !important;
}

#menubar .navbar {
  padding-left: 0 !important;
}

.navbar.navbar-dark {
  height: 44px;
  margin: 0 15%;
}

nav.navbar .navbar-nav li.nav-item.active:after {
  content: "";
  position: relative;
  margin-left: -31px;
  left: 50%;
  bottom: 15px;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 6px solid white;
}


/*submenu*/

#menubar li.dropdown.show {
  position: static;
}

#menubar li.dropdown.show .dropdown-menu {
  display: table;
  width: 100%;
  text-align: center;
  left: 0;
  right: 0;
  margin: 0;
}

.dropdown-menu>li {
  display: table-cell;
  padding-top: 6px;
}

.dropdown-menu>li a {
  font-weight: 600;
}

.dropdown-menu>li a:hover {
  text-decoration: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<nav class="navbar navbar-expand-md navbar-dark ">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#menubar">
                        <span class="navbar-toggler-icon"></span>
                    </button>
  <div class="navbar-collapse" id="menubar">
    <ul class="nav navbar-nav">
      <li class="nav-item ">
        <a class="nav-link" href="#">Menu1</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link" data-toggle="dropdown" href="#">Menu2</a>
        <ul class="dropdown-menu" role="menu">
          <li><a href="#">Submenu1</a></li>
          <li><a href="#">Submenu2</a></li>
          <li><a href="#">Submenu3</a></li>

        </ul>
      </li>

    </ul>
  </div>
</nav>

Answer №1

In order to better suit your needs, consider modifying the hover event from the link to the list item. This adjustment is a widely adopted method that may be beneficial in your scenario:

$("#menubar li.dropdown").hover(function() {
    ...
});

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

Icons on Speed-Dial are not aligning to the center

I've been working on creating a Speed Dial feature for a web application, but I can't seem to get the icons to align properly in the Speed Dial menu. Despite following the instructions provided in https://vuetifyjs.com/en/components/floating-act ...

What could be causing NPM to generate an HTTP Error 400 when trying to publish a package?

My current goal is to release an NPM package named 2680. At the moment, there is no existing package, user, or organization with this specific name. Upon inspection of my package.json, it appears that everything else is in order. Here are all the relevant ...

The Dropbox picker is now launching in a new tab instead of a new window when using Chrome

I am encountering an issue with opening Dropbox picker in a new modal window. It works perfectly in all browsers except for Google Chrome. Is there anyone who can guide me on why it opens in a new tab in Chrome? Is there a parameter in options that I can ...

What is the best way to execute a jQuery function following a redirect triggered by a form submission?

Currently, I am faced with an issue: I am using rails, devise gem, and jQuery validation plugin. After registration, there is a path that leads to another page, but I want to display a modal with a congratulatory message afterwards. The problem is that the ...

Files with extensions containing wildcards will trigger a 404 error when accessed from the public folder in NextJS

I have successfully set up my public folder to serve static files, however I am encountering an issue with files that have a leading dot in their filename (.**). Specifically, I need to host the "well-known" text file for apple-pay domain verification, wh ...

What's the reason behind CSS selectors not allowing empty spaces, while XPath does?

Why is it that Xpath is able to find an HTML element with a class attribute containing empty spaces, while CSS cannot? # Element: <div class="I Love StackOverFlow"></div> self.page.locator('//div[@class="I Love StackOverFlo ...

Searching within a collection for a specific condition using a Mongoose query

I'm trying to clarify the title of this question, so let's skip directly to the schema: const UserSchema = new Schema({ name: String, _events: [{ type: Schema.Types.ObjectId, ref: "Event" }] }); Essentially, a user can have multiple events ...

What is the correct way to outline the parameters for deactivating functions?

Request for Assistance! I am facing a challenge with multiple blocks in my WordPress website. Each block contains a checkbox, two select options, and an element that needs to be toggled based on the selected options in the dropdowns. The functionality to ...

Combine filter browsing with pagination functionality

I came across a pagination and filter search online that both function well independently. However, I am looking to merge them together. My goal is to have the pagination display as << [1][2] >> upon page load, and then adjust to <<[1]> ...

How can I modify a dynamically generated table to include rowspan and colspan attributes in the rows?

My table was automatically created using data from the database. var rows = ""; rows += "<tr class='row_primary'>"; rows += "<td>COL 1</td>"; rows += "<td>COL 2</td>"; rows += "<td> ...

What is the process for exporting Three.js files to .stl format for use in 3D printing?

I stumbled upon a page with this link: Is it possible to convert Three.js to .stl for 3D printing? var exporter = new THREE.STLExporter(); var str = exporter.parse(scene); console.log(str); However, despite using the code provided, I am unable to export ...

Is it possible to override values set in the constructor(props) in React? If not, what is the best way to set defaults that can be overwritten later?

I'm confident I know the solution to this problem because my setState({}) seems to have no effect. This is the constructor code that I currently have: constructor(props) { super(props); this.state = { percentiles: { incN ...

Check to see if the property of the object does not exist within the array and update as

My goal is to add the variable content into the database content using the $push method, but only if the content.hash doesn't already exist in the database. I want to avoid duplicating information unnecessarily. return shops.updateAsync({ "user": u ...

The replaceWith() function in jQuery is able to transform PHP code into an HTML comment

How can I change a div element in a click event handler, while ensuring that PHP code inside the element remains intact and does not get moved into an HTML comment? You can find my code snippet below: $("#replaceCat1").click(function(){ $("div.boxconte ...

Unexpected CSS counter reset issue encountered within nested elements

Utilizing css counters for the formatting of certain wiki pages is a common practice that I implement by adding CSS directly to the wiki page. One particular use case involves numbering headers using counters. Below is a snippet of the code that demonstra ...

"Encountering a restricted URI error when attempting to load a text file using AJAX

After reviewing the recommended link, I found myself struggling to grasp the suggestion to "Use Greasemonkey to modify Pages and start writing some javascript to modify a web page." Currently, I am encountering an error when loading a text file using $.aj ...

Ensuring the modal is stored in one central location (Bootstrap)

I'm currently working on a website project using bootstrap, and I need to incorporate the same modal across all pages. Right now, I find myself duplicating the entire code in each HTML file to make it accessible on every page. However, this method is ...

Having trouble with button alignment in the header with Bootstrap 3?

Using Bootstrap 3 and looking to adjust the alignment of a star icon with the title in my HTML page. Here is the current setup: <p class="pull-right visible-xs"> <h3><center>2000 Cadillac Eldorado Esc (Prospect heights) $3500 ...

Disorganized HTML Elements

If you visit diartefloral.tech and use a cellphone in the "Sobre Nosotros" section of the menu, the footer overlaps an image. I'm not sure how to fix this issue. The most relevant code is shown below; I am using Bootstrap. Previously, I had an issue w ...

Contact form script malfunctioning, showing a blank white page

Encountering a white screen when trying to submit my contact form with fields for Name, Email, Subject, and Message. I am looking to receive emails through my website. I have double-checked all variable names and everything seems to be correct. Since I am ...