Solved

How to prevent jquery dropdown menu from closing when mouse moves out of layer

Posted on 2014-10-31
7
414 Views
Last Modified: 2014-10-31
    <!-- BEGINNING OF MENU -->  

		<!--<ul class="dropdown" style="z-index:1;margin-left:15px !important;">-->
		<ul class="dropdown" style="z-index:1;margin-left:10px !important;">
        	<li>	
 	             <table border=0><tr><td><img 

Open in new window


I have a complicated navigation menu with more then 3 steps (menu->submenu->subsubmenu), I want to make it so any menu or submenu opens on mouseover or click (as it does now) but does not close if the mouse goes out outside the range of the menu/submenu...

How can I achieve this..

Thanks
0
Comment
Question by:Mark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40415781
What would be the mechanism for hiding the menu again?
0
 

Author Comment

by:Mark
ID: 40415790
If the mouse leaves the menu boundary, the menu/submenu/... should remain open for half a second giving the user a chance to move their mouse back onto the menu incase they accidentally moved their mouse outside the boundary while trying to navigate the menu
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40415832
Then you have to bind to the mouseover event for the <li> and set a class on the li to show the menu and submenu.

At the same time you need to start a timer (setTimeout) which if it fires hides the menu - on the mouseover event you would then clear the timer to prevent it firing.

The question is do you want this behaviour to be only on the sub-sub menu - or the sub menu or progressively i.e. if over main menu - that becomes sticky - then if go over sub-sub menu both become sticky?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:Mark
ID: 40415840
progressively
0
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40415863
Then you would need something like the code below. Guessing here because don't have your code - you would need to create (replace) the .open class and style it to show your menu items.
(untested - so you might need to tweak)
$(function() {
   var handleTimeout = null; // HANDLE TO HOLD TIMEOUT
   var TIMEOUT=300;   // TIMEOUT LENGTH

   // INTERCEPT MOUSEOVER ON <li> ELEMENTS
   $('ul.dropdown li').mouseover(function() {
      // CLEAR THE TIMEOUT IF IT EXISTS
      if (handleTimeout != null) clearTimeout(handleTimeout);
      // ADD A CLASS TO THE MENU ITEM TO OPEN IT
      $(this).addClass('open'); // Change class as needed and style
 
      // SET A TIMEOUT TO HIDE MENU AFTER TIMEOUTms
      handleTimeout = setTimeout(function() {
         $('li.open').removeClass('open');
         handleTimeout = null;
      }, TIMEOUT);
  });
});

Open in new window

0
 

Author Closing Comment

by:Mark
ID: 40415873
Thanks!
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40415899
You are welcome - good luck with it.
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question