"Enhancing Your Website Navigation with jQuery: Incorporating Mouse Events into Your Menu

My objective is to create a simple mouseover effect on my menu that remains active while the mouse is in a submenu, and triggers the close() function when the mouse leaves the main tab or the submenu.

I understand that an event handler is required to trigger the close() function, and I would greatly appreciate any advice or assistance on this matter.

Currently, the jQuery script rewrites the parent class 'cbp-hrclosed' to 'cpb-hropen'.

For reference, here is an example of what I am trying to accomplish: http://tympanus.net/codrops/2013/03/05/horizontal-drop-down-menu/

The jQuery

var cbpHorizontalMenu = (function() {

    var hover = 1;

    var $listItems = $( '#cbp-hrmenu > ul > li' ),
        $menuItems = $listItems.children( 'a' ),
        $body = $( 'body' ),
        current = -1;

    function init() {
        $menuItems.on( 'click', open );

        $listItems.on( 'click', function( event ) { event.stopPropagation(); 
        hover=1;
        } );
    }

    // Rest of the jQuery script goes here...

})();

HTML

<?php echo 'HTML code here'; ?>

CSS

/* CSS code goes here... */

Answer №1

If you want to make your menu creation process simpler, consider using pure css for this task.

For instance, by utilizing an unordered list along with the following css code, you can achieve the desired outcome without the need for jquery.

HTML

<ul><li>Home</li>
  <li>
    Antique
    <ul>
      <li>Heading</li>
      <li>Heading</li>
      <li>Heading</li>
    </ul>
  </li>
  <li>
    Modern
  <ul>
      <li>Heading</li>
      <li>Heading</li>
      <li>Heading</li>
    </ul>
  </li>
  <li>
    Contemporary
  <ul>
      <li>Heading</li>
      <li>Heading</li>
      <li>Heading</li>
    </ul>
  </li>
  <li>
    Exhibitions
  <ul>
      <li>Heading</li>
      <li>Heading</li>
      <li>Heading</li>
    </ul>
  </li>
</ul>

CSS

body {
  font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  padding: 20px 50px 150px;
  font-size: 13px;
  text-align: center;
  background: #E3CAA1;
}

ul {
  text-align: left;
  display: inline;
  margin: 0;
  padding: 15px 4px 17px 0;
  list-style: none;
  -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
  -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
}
ul li {
  font: bold 12px/18px sans-serif;
  display: inline-block;
  margin-right: -4px;
  position: relative;
  padding: 15px 20px;
  background: #fff;
  cursor: pointer;
  -webkit-transition: all 0.2s;
  -moz-transition: all 0.2s;
  -ms-transition: all 0.2s;
  -o-transition: all 0.2s;
  transition: all 0.2s;
}
ul li:hover {
  background: #555;
  color: #fff;
}
ul li ul {
  padding: 0;
  position: absolute;
  top: 48px;
  left: 0;
  width: 150px;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  display: none;
  opacity: 0;
  visibility: hidden;
  -webkit-transiton: opacity 0.2s;
  -moz-transition: opacity 0.2s;
  -ms-transition: opacity 0.2s;
  -o-transition: opacity 0.2s;
  -transition: opacity 0.2s;
}
ul li ul li { 
  background: #555; 
  display: block; 
  color: #fff;
  text-shadow: 0 -1px 0 #000;
}
ul li ul li:hover { background: #666; }
ul li:hover ul {
  display: block;
  opacity: 1;
  visibility: visible;
}

I trust this information proves useful. This type of css menu was employed in my project, making it easy to customize for various requirements.

For a live example, visit:

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

The autocomplete feature in Jquery tagsinput is malfunctioning within a Bootstrap modal

Currently, I am working on implementing an autocomplete tag search within a bootstrap modal. The jquery library that I am using for this purpose can be found at this link. While the autocomplete search feature functions properly when the input textbox i ...

Incorporate parameters and data attributes with ScriptInjector in GWT

Is there a way to pass the id=someScript and param1=value1 to ScriptInjector in GWT? For example, consider the following JS code: <script src="https://blah.com/someScript.js" id="someScript" param1="value1"></script> In GWT, you can load it l ...

Utilize prop-types inheritance when a component is rendered via props

Is it possible to inherit prop-types when a component is rendered via the parents prop, without direct access to 'ChildProps' and 'Props' interface? Parent Component interface ChildProps { counter: number; setCounter: React.Dispat ...

The preventDefault() function is not functioning properly on the <a> tag

As a JavaScript beginner, I decided to create an accordion menu using JavaScript. Although I was successful in implementing it, I encountered a bug in my program. In this scenario, uppercase letters represent first-level menus while lowercase letters repr ...

What is the best way to adjust the size of an image within a block layout using either JavaScript or React?

I have a game to create, and I need a block of elements to be aligned like the image below. However, the kangaroo image is not displaying correctly. The actual size of the image is width:70px and height:100px. But I want to resize it to width: 49px and h ...

The event was triggered, however, some sections of the code did not run

I am currently working on a project called lan-info on GitHub. Here is the code snippet that I am using: let arpSweepCommand = './arpSweep.sh'; app.get('/arp', (req, res) => { console.log('we have a working signal!'); ...

How can you set the listbox in Sumo Select to always be open?

https://i.sstatic.net/fkiHB.png Is there a way to ensure the listbox is always open by default, as if the user had clicked? ...

Incorporating VueJS with a sleek material design aesthetic

Currently, I am in the process of developing a web application using VueJs and am in need of a CSS framework to aid in designing without starting from scratch! After some research, I came across material-design-lite (www.getmdl.io) but unfortunately faced ...

The process of dynamically populating data into a select element through an Ajax request using JavaScript may seem complicated at

I am working on an html page that includes two select options. The values for these select options are retrieved from a Servlet via an ajax call in the onclick() function. However, I am encountering an issue where the data is not populating into the sele ...

Is it possible to update only the inner text of all elements throughout a webpage?

Scenario After coming across a thought-provoking XKCD comic, I decided to craft a script that would bring the comic's concept to life: javascript:var a=document.getElementsByTagName('body')[0].innerHTML;a=a.replace(/Program(\w\w+ ...

Are there any alternative methods for clearing form fields following a successful thunk dispatch in React?

When implementing a Post API call in Thunk, I dispatch a boolean success key for successful requests and an error message for errors. Now the goal is to clear form data upon success and display the error message upon an error. To achieve this, I utilize ...

Display drop-down item when selected for the first time and hide it when selected for the same item for the second time

Is there a way to display form input when "C" is selected and hide it when "A", "B", or "D" are selected? If "C" is selected again after initially showing the form input, should it be hidden? For example, if "C" is selected for the first time, the form inp ...

The Double Negation operator

While reading a book, I came across this code snippet: !!(document.all && document.uniqueID); I'm wondering why the double not operator is used here. Doesn't the && operator already convert the result to a Boolean? ...

Utilizing jQuery Mobile and PhoneGap to incorporate HTML files into data-role=page: A step-by-step guide

I am looking to load HTML files into a data-role=page using jQuery Mobile and PhoneGap. My project consists of several small HTML files with separate pages. In my implementation, I have: index.html: <!DOCTYPE html> <html> <head> ...

Designing an HTML and CSS footer navigation bar

Can you help me create a footer menu using HTML and CSS? Check out this image for reference Any tips on how to style it with cascading style sheets? <footer> <ul> <li><a href="">Home</a> </li> <li> ...

Tips for positioning tables on a page

I have 4 tables and a submit button. How can I make them more compact? Maybe like this: </img> This is my HTML code: <body> <form action="/cgi-bin/form.py" id="myform"> <table class="table-fill"> ... ...

How to access previous Redux state when navigating back using the Back button

Imagine a search page equipped with a search bar and various filters for the user to customize their data retrieval. As the user selects different filters, an API call is made to fetch updated data and the route path is also adjusted accordingly. However ...

Transferring a JavaScript variable to PHP using Ajax within the same webpage

Check out my HTML and JavaScript code: <form id="form" action="javascript:void(0)"> <input type="submit" id="submit-reg" value="Register" class="submit button" onclick="showtemplate('anniversary')" style='font-family: georgia;font- ...

An array of dictionaries that are the same except for one dictionary containing an unspecified key

A while ago, I wrote some sample code to generate Euler diagrams from an array of dictionaries. Strangely enough, it only works when the array is explicitly defined in my .js file; using input from a .json file to construct it incrementally does not yield ...