Simple Animated Menu for Mootools 1.2

- Troy

Curious how to build something like the menu featured on this site for your next web design project? (note IE6 users - the menu is not implemented for you as your browser does not properly support png transparency, but with a solid background, it would work great). To make the menu, you won’t even have to muck about in javascript. Simply define:

  • a container for the background image
  • the nav elements
  • the ‘current’ nav element ID
  • the animation’s horizontal track (the y coordinate of the background-image)
  • the left offset of the image in relation to the nav element

Basically, you just tell the function where to put the background image you want to animate, what the buttons are and how to identify the current id, then position its x and y coordinates in relation to the elements. If you are new to javascript web development, and not sure what this all means, keep reading. It breaks down very simply.

Installation

Install mootools 1.2, then copy and paste the code before the </head> tag in your html file, or save it as a .js and pull it in.

  1. Download mootools 1.2, in my opinion, the best javascript library around.
  2. Upload mootools 1.2 and link to it in the head tag (<script src=”/js/mootools1.2.js” type=”text/javascript”>)
  3. Place the menu code between script tags (<script type=”text/javascript”> </script>), before the end of the head tag in your html (or)
  4. Save the code as simple_animated_menu.js (or whatever you like) and link to in the head tag (<script src=”/js/animated_moomenu.js” type=”text/javascript”>)

Configuration

Javascript

You can configure the the code to work with your current menu by defining the options when calling the navArrowSlider() function.

  1. Define the ID of the nav wrap
  2. Define the navigation elements
  3. Define the ID you use to denote the current tab
  4. Define the background y position (the horizontal track for the image).
  5. Define the left offset of the image in relation to the nav element (integer only, no “px” necessary)
navArrowSlider(
	'nav_wrap', // ID of nav wrap
	'#nav_wrap ul li', // Array selector of nav elements
	'active_nav', // ID of current nav element
	'20px', //  Background position y of background image
	'20' //  INT ONLY - How far left from the right edge of the nav element that the image settles
);

HTML

To use this code as a template for a new web design, you can copy and paste the following code.

<div id="nav_wrap">
 
<ul>
 
	<li id="active_nav">Tab 1</li>
 
 
	<li>Tab 2</li>
 
 
	<li>Tab 3</li>
 
 
	<li>Tab 4</li>
 
</ul>
 
</div>

CSS

Aside from your standard menu styles, you will need to add:

#nav_wrap {
	height: 100px; /* however tall it needs to be to fit your background image */
	width: 100%;
	background-image: url(/images/fancy_menu_image.png);
        padding-top: 50px; /* gives the image room to be viewed, pushing the ul down */
}

Notes:

This code has been tested in FF 3.0, FF 2.0, IE 7, IE 6 (minus image transparency), Safari for Windows and Opera 9.5.

Be sure to keep the ‘domready’ function around navArrowSlider() as mootools requires it to run.

The image will only return to the active nav element when you leave the ‘nav_wrap’ area. This lets you control when the image snaps back - for example, you can have it snap immediately when they leave the tabs by setting ‘nav_wrap’ to ‘width: auto; height: auto;’.

You can use the code anyway you like, and I would appreciate you letting me how if you use it your next web design project and what you think.

Hope you find this useful, and I look forward to hearing any feedback, questions or other comments.

Download a zip of all necessary files

*Including the mootools 1.2 core, the ’simple animated menu’ javascript file, an html menu file, a simple css file and an image.

Or you can copy and paste the code below.

var navArrowSlider = function(navWrap, navElementsArray, activeID, arrowY, leftOffset) {
	var youAreHere = new Fx.Tween($(navWrap), {
		duration: 1200,
		transition: Fx.Transitions.Elastic.easeOut  //adjust transition effect here
	});
 
	$$(navElementsArray).each(function(item){
		item.addEvent('mouseenter', function() {
			var thisPos = item.getPosition(navWrap).x  + item.getSize().x - leftOffset;
			youAreHere.cancel();
			youAreHere.start('background-position', thisPos + 'px ' + arrowY + 'px');
		});
	});
 
	var currentArrow = function() {
		youAreHere.cancel();
		var activePos = $(activeID).getPosition(navWrap).x  + $(activeID).getSize().x - leftOffset;
		youAreHere.start('background-position', activePos + 'px ' + arrowY + 'px');
	};
 
	//correct IE rendering problem (without this, it wont go to the active nav onload)
	var activePos = $(activeID).getPosition(navWrap).x  + $(activeID).getSize().x - leftOffset;
	$(navWrap).setStyle('background-position', activePos + 'px ' + arrowY + 'px');       
 
	//works to set image to starting position in other browsers
	currentArrow(); 
 
	$(navWrap).addEvent('mouseleave', currentArrow);
}; 
 
window.addEvent('domready', function() {
	navArrowSlider(
		'nav_wrap', // ID of nav wrap
		'#nav_wrap ul li', // Array selector of nav elements
		'active_nav', // ID of current nav element
		'20px', //  Background position y of background image
		'20' //  INT ONLY - How far left from the right edge of the nav element that the image settles
	);
});

Creative Commons License
This work is dedicated to the Public Domain.

Bookmark and Share

Comments (44) Trackbacks (22)
  1. janlee


    i like this one. very simple too, so why use flash for web anymore when mootools has enough power?
  2. lauren


    I'm with you, janlee. I used to be a big fan of Flash (and it still has its uses) but Troy has converted me. I see the javascript light.
  3. insic


    Very nice. I use mootools a lot. i love this framework... thanks for this very cool example.
  4. Raul


    wow! looks great
  5. Justin


    Hi! Love it!
    Just one problem.
    I came across an annoying bug where the starting point of the background image varies in firefox on both MAC and PC. If you navigate over any menu link the positioning is rectified.
    As I am not a JavaScript GURU I was hoping someone may be able to looking to this.
    It's not as visible with the example on this page bu t if you are using a smaller image it becomes quite apparent. Thanks for any help in advance!
  6. Justin


    My mate has updated the script to have a left margin offset in Px. which works with the CSS position of my drop down menu.
    Start position has been added to fix the background image position bug in Firefox 2 on both MAC and PC. Final installation will be visible here shortly:
    www.falcommercial.com.au

    Cheers : )

    UPDATE SCRIPT BELOW
    ----------------------------------

    var navArrowSlider = function(navWrap, navElementsArray, activeID, arrowY, leftOffset) {

    var youAreHere = new Fx.Tween($(navWrap), {
    duration: 300,
    transition: Fx.Transitions.Quad.easeInOut
    });

    $$(navElementsArray).each(function(item){
    item.addEvent('mouseenter', function() {
    //var thisPos = item.getPosition(navWrap).x + item.getSize().x - leftOffset;
    var thisPos = item.getPosition(navWrap).x + 10;
    youAreHere.cancel();
    youAreHere.start('background-position', thisPos + 'px ' + arrowY + 'px');
    });
    });

    var currentArrow = function() {
    youAreHere.cancel();
    //var activePos = $(activeID).getPosition(navWrap).x + $(activeID).getSize().x - leftOffset;
    var activePos = $(activeID).getPosition(navWrap).x + 10;
    $(navWrap).setStyle('background-position', '10px ' + arrowY + 'px'); //Fixes positioning in Firefox 2
    youAreHere.start('background-position', activePos + 'px ' + arrowY + 'px');
    };

    //correct IE rendering problem (without this, it wont go to the active nav onload)
    //var activePos = $(activeID).getPosition(navWrap).x + $(activeID).getSize().x - leftOffset;
    var activePos = $(activeID).getPosition(navWrap).x + 10;
    $(navWrap).setStyle('background-position', activePos + 'px ' + arrowY + 'px');
    //works to set image to starting position in other browsers
    currentArrow();

    $(navWrap).addEvent('mouseleave', currentArrow);
    };
  7. Troy


    Awesome Justin, I'll be sure to update the script on the page shortly. Look forward to seeing it in action.
  8. steve


    very nice... great work!
  9. PKhunter


    Hi. This looks superb. Do you know how we could customize it to look like panic.com's smooth animated appearance on mouseover?
  10. Troy


    PKhunter, I checked out the link but didn't see anything animated. Could you point me to what you were referring to?
  11. demo


    Dude, put up a demo. I want to see the script in action before I bother with it.
  12. Troy


    demo,

    The nav of our site uses this script, as we explain in the first paragraph.
  13. toastedpixel.org


    This is great thanks!
  14. Designer


    wow... thanks a ton....... it looks great
  15. Abba Bryant


    Any chance of making this into a MooTools 1.2 Class?

    If you aren't interested, or don't have the time, would you be alright with me doing the conversion and perhaps reposting it back here for you guys?
  16. Troy


    That is on my todo list. I plan on releasing something within the next few weeks, but you are more than welcome to have a go at it. Please do drop a note with anything you come up with :)
  17. peter


    Hi Troy,

    I really like this.
    I am trying to have the arrow jump to a "generic" space on the right if no ActiveID is set (i.e. above a logo)... is this possible ?
    I am currently trying to see if activeID is set, and if empty, give activePos hardcoded values... but I must be doing something wrong..
  18. Troy


    Peter, you could always put your logo in a UL with your nav, then hardcode the ActiveID to that li. If I am picturing what you are talking about correctly, that would solve it.
  19. Harry


    nice animation, i will use it on my blog
  20. Troy


    Thanks Harry, that make me smile, I dig it :)
  21. Raphael


    Hello Troy,

    Nice work. I am French, so excuse my English of school.
    I use another compatible script mootools 1.2 ( MooFlow), and if I put it in action with your menu under Internet Explorer 7, and it doesn't work. If I remove your script, that works.

    No problems with FF and Opera.

    What can be the problem? I changed the order of invocation of the scripts but that changes nothing under IE.

    You can see my problem =&gt; http://www.j-pop-spirit.com/ana

    Thank you
  22. Troy


    Raphael, try using the modified code posted in the comments above. I will be releasing a new version shortly, which should deal with this sort of issue.
  23. Raphael


    I tried with Justin's Code and it doesn't works on IE7. (With your code, it works in IE6).

    With Justin's code, the effect of bounce does not exist any more. The image is only moving without bouncing.
  24. d2f


    I´m trying to use a class (active /inactive) instead of an ActiveId, cause I don´t recharge the page to show the arrow over the current active tab (due to I´m just scrolling between id´s to show new content), but I can´t get it to work. Any idea would be appreciated.

    By the way, marvellous effect :-)
  25. Troy


    d2f - you could modify the function the accept a different selector other than an ID, but I will be releasing a new version of this shortly and will take that into consideration when I do.
  26. Raphael


    Hello troy,

    I found my problem. There was several window.addEvent (Mooflow, Simple Animated Menu:p)) and IE7 accepts only one.

    Perfect.

    Bravo ;)
  27. Miles


    I am trying to add this to my wordpress theme. But nothing is actually animated. I see the arrow but it will not move.

    I'm pretty sure mootools isn't installed correctly.

    Can anyone offer help on how to install mootools correctly into a wordpress theme?
  28. rhio


    thank you for your project....
  29. Joe


    This is a very cool demo.

    Please don't hate me for asking this but...does anyone out there know how to pull off an effect like this using jQuery instead of MooTools? I don't want to mix my libraries.
  30. Troy


    Joe, I am not sure if there is anything like this for jquery specifically, tho a few people have said that they were going to port the code. I'll be sure to post if I hear anything solid.
  31. Peter


    Hello Troy ,

    Very Nice script , the effect is awersome !

    But i'm with the same problem of Raphael , i've just downloaded the zip patch and it doesn't work at IE7 ... I've Tried with Justin's code too but it still doesn't work..

    Thx
  32. Liam


    Cool Menu!

    Is there a way of stopping the arrow from moving from side to side and then settling. Ie Is it possible to just make it move to one of the navigation buttons and stop dead?

    Also, is it possible to have the arrow align with the center of each navigation button instead of to the right?

    Cheers, Liam
  33. Steve Yakoban


    I saw an implementation like this but instead of the buttons switching, the buttons had a brief but full animation.

    Any idea on how a javascript novice (me!) can modify this to do that?
  34. Nadia


    I am using this for an asp website, when I test in html it works fine, but testing to the server nothing works, is there something I can use to fix this?
  35. Internetagentur


    Thank you .... this menu tutorial with mootools has me very helped.
  36. Sleepytoast


    This is an awesome tutorial, but I have a problem. Other than being completely new to javascript and mootools. I would like this to work for a vertical navigation. How could I accomplish this if at all possible?
  37. Troy


    Sleepytoast - I would check out morphlist, it's a much more developed script and works vertically:
    http://devthought.com/projects/mootools/morphlist/
  38. Baja Real Estate


    I was looking for an easy to follow tutorial. This one delivered! Thanks!
  39. Chest Rockwell


    Why does this animation work in IE when I view this site, but the ZIP file doesn't work?
  40. Valerio


    hi,first of all thanks for the script, I'm using on a website I'm creating for an english band. The address is: www.killitkid.netsons.org.
    I had to disable the Joomla mootools 1.1 library in frontend to use the required mootools 1.2 one. However, now the menu is working but I noticed two important bugs, the first is that the pointer under the menu doesn't settle under the active page but it always return under the home button. And the second is that when I click on the media button the menu doesn't work, I thought that the problem is the flash gallery I use in this section.
    Can you help me? Thanks :D
  41. Joe


    Hi there. Could anyone help me I say why do I have error on page when visiting my site with IE 8 ? It says that I have an invalid argument in mootools 1.2.js line 2019 and also object does not support this property or method, same .js file, line 2704. I have no errors when viewing it in any other browser. I'm not a js programmer there4 I have no idea how to fix this. Thnx in advance. (www.herbavit.net)
  42. Mike


    Hi there,

    First things first I love this menu, you can do so much just by changing the transition effect and whatnot. I do however have a small problem, the website im trying to implement this on doesnt have any IDs set on its menu system, i would like to change the code to look for an "active" class as opposed to the "active" ID its currently looking for but cant seem to get it right, any assistance would be greatly appreciated.

    I have tried li.active and other derivatives of this to no avail.

    regards,

    Mike
  43. Bob


    great script! thnx!
    but a question: i wanna change the z-index of the arrow, but it dosnt work.
    i want that the arrow is OVER the menu.
    please help
    thnx
  44. Cobus


    I have implemented this, and it is great. Thanks for your good work.


Leave a Reply