[solved] Fading in and out a sub nav with a hover trigger

here you will find only solved Help topics

Moderator: 1.2 Moderators

[solved] Fading in and out a sub nav with a hover trigger

Postby BostonMark on Mon Jul 28, 2008 5:29 am

Hi all,
I'm creating a navigational sub-menu that is trigger by a parent image. Here's how I want it to work:

IMAGE HERE
Sub nav:
- 1
- 2
- 3
- etc...

On mouseover of the image, it will fade the subnav in.
On mouseout of the image, it will fade the subnav out HOWEVER if you are on the actual sub-nav itself, don't fade out.

Right now, I have a loop to set the fades for in and out for the image, but I can't figure out how to make the fade out not happen when you're on the sub-nav itself.

  1.  
  2. // set the fade time and initially hide sub navs
  3. theSubNav.get('tween').options.duration=1000;
  4. theSubNav.fade('hide');
  5.  
  6. // hover on causes sub nav to fade in
  7. theHover.addEvent('mouseover', function(event) {
  8.     event = new Event(event).stop();
  9.     theSubNav.fade('in');
  10.  
  11. });
  12.  
  13. // hover off causes sub nav to fade out
  14. theHover.addEvent('mouseout', function(event) {
  15.     event = new Event(event).stop();
  16.     theSubNav.fade('out');
  17.  
  18. });
  19.  


Any tips on making that last event (mouseout) happen only if "theSubNav" isn't being hovered upon itself.
Last edited by BostonMark on Sat Aug 09, 2008 10:26 pm, edited 1 time in total.
BostonMark
 
Posts: 3
Joined: Mon Jul 28, 2008 5:28 am

Re: Fading in and out a sub nav with a hover trigger

Postby keif on Mon Jul 28, 2008 5:47 am

Rough idea:

Include in the moustout code a detection for the subnav (so, if mouseout && !subnav, fade out).

Do you have an example online?
-keif
blog
keif
mootools fan
 
Posts: 58
Joined: Wed Jul 16, 2008 5:18 am
Location: Columbus, OH

Re: Fading in and out a sub nav with a hover trigger

Postby BostonMark on Mon Jul 28, 2008 6:06 am

I don't have a sample online and I cannot really put something working up yet.

I did try what you're saying myself, like this in pseudo-code:

hover image: add mouseover event {
nothing changes form previous code
}
hover image: add mouseout event {
check flag variable as mentioned below
}
sub-menu: add mouseover event {
set a flag variable
}

The problem is that the image mouseout event occurs before the sub-menu mouseover event occurs.
BostonMark
 
Posts: 3
Joined: Mon Jul 28, 2008 5:28 am

Re: Fading in and out a sub nav with a hover trigger

Postby X-trace on Mon Jul 28, 2008 9:15 am

Create a div that contains the navigation and the subnavigation. Add the event handler to the wrapper div.
The subnavigation is placed inside the wrapper, so it will fade out after mouseleave on wrapper.
User avatar
X-trace
mootools enthusiast
 
Posts: 399
Joined: Wed Jul 16, 2008 4:23 pm
Location: Groningen, NL

Re: Fading in and out a sub nav with a hover trigger

Postby Enric on Mon Jul 28, 2008 8:29 pm

Hi,

I made a drop down menu with mootools 1.2, here is the code:

HTML:
  1. <ul id="botones_menu">
  2.         <li><a href="#">Menu 1</a>
  3.           <ul>
  4.             <li><a href="#">submenu 1</a></li>
  5.             <li><a href="#">submenu 1</a></li>
  6.             <li><a href="#">submenu 1</a></li>
  7.             <li><a href="#">submenu 1</a></li>
  8.             <li><a href="#">submenu 1</a></li>
  9.             <li><a href="#">submenu 1</a></li>
  10.           </ul>
  11.         </li>
  12.         <li><a href="#">Menu 2</a>
  13.           <ul>
  14.             <li><a href="#">submenu 2</a></li>
  15.             <li><a href="#">submenu 2</a></li>
  16.             <li><a href="#">submenu 2</a></li>
  17.             <li><a href="#">submenu 2</a></li>
  18.           </ul>
  19.         </li>
  20.         <li><a href="#">Menu 3</a> <!-- No submenu here -->
  21.         </li>
  22.         <li><a href="#">Forum</a> <!-- No submenu here -->
  23.        </li>
  24.         <li><a href="#">Stadistics</a>
  25.           <ul>
  26.             <li><a href="#">Sta 1</a></li>
  27.             <li><a href="#">Sta 2</a></li>
  28.             <li><a href="#">Sta 3</a></li>
  29.           </ul>
  30.         </li>
  31.       </ul>


And the js:
  1. $$('ul#botones_menu > li')
  2.   .each( function(el, ind) {
  3.     var sub_el = el.getElement('ul');
  4.     if (sub_el) {
  5.       sub_el.set('opacity', 0);
  6.       el.addEvent('mouseenter', function(){
  7.         sub_el.fade('in');
  8.       });
  9.       el.addEvent('mouseleave', function(){
  10.         sub_el.fade('out');
  11.       });
  12.     }
  13.   });


Next step is to play with the CSS, if you want I can post a working example.

Bye
User avatar
Enric
 
Posts: 8
Joined: Wed Jul 16, 2008 7:15 pm

Re: Fading in and out a sub nav with a hover trigger

Postby BostonMark on Wed Jul 30, 2008 5:04 am

Hi all. Thanks for the replies. I thought up the same idea as X-trace and did that. I just remembered I posted here though.

I was not aware, however, of the mouseenter and mouseleave events. In fact... where are they documented in MooTools? I recently searched the Docs for events and couldn't find a definitive list. The only thing I saw was a link to http://www.w3schools.com/html/html_eventattributes.asp

Thanks for the help all
BostonMark
 
Posts: 3
Joined: Mon Jul 28, 2008 5:28 am

Re: Fading in and out a sub nav with a hover trigger

Postby keif on Wed Jul 30, 2008 5:33 am

http://ikeif.net/2008/07/29/mootools-mouse-events/

So I can build a few more links to the blog:

The list you've found are "javascript events" - the mootools script removes the "on" part of the event - it doesn't need to be documented, because it's a slight syntax, but it's just assumed that the users understand javascript events, which is why they don't re chronicle javascript events because it'd look like:

"mouseover" just like "onmouseover"
"click" just like "onclick"

If you need more details, let me know or comment on the blog :)
-keif
blog
keif
mootools fan
 
Posts: 58
Joined: Wed Jul 16, 2008 5:18 am
Location: Columbus, OH

Re: Fading in and out a sub nav with a hover trigger

Postby Enric on Thu Jul 31, 2008 7:23 pm

Hi,

I upload an example, you can take a look to the css there also. => http://www.jpvincent.com/moomenu/

For your case, you can put IMG tags in the main menu.

Moreover I think the lists are more appropriate for menus.

Bye.
User avatar
Enric
 
Posts: 8
Joined: Wed Jul 16, 2008 7:15 pm

Re: Fading in and out a sub nav with a hover trigger

Postby ali on Fri Aug 01, 2008 9:34 am

Thank you Enric.
It works :)

I find a trick,
i test and find that, in a page I can use one menu. and if want to use two independent menu in a page, the second not work.
I find the solution too, if you describe class instead of id in the css file, it works. i mean, using code like "ul.botones_menu" instead of "ul#botones_menu" in the css and js file, and use <ul class='botones_menu'" instead of "<ul id='botones_menu'" . you can use several independent menus in one page.

I have a question. I want that the menus have not same width, and their width be depend on the menu text.
how can I do that?
ali
 
Posts: 1
Joined: Fri Aug 01, 2008 9:19 am


Return to Solutions

Who is online

Users browsing this forum: Ask Jeeves [Bot] and 2 guests