This blog has moved here: woorkup.com | FOLLOW ME ON TWITTER @woork
Wednesday, April 9, 2008

Nice animated menu using CSS and Mootools

This tutorial illustrates how to implement a nice animated menu using Mootoolsand some lines of CSS and HTML code ready to reuse in your project.

This is the basic version with a basic design, but you can customize it how you prefer modifying the CSS related source code.


Download this tutorial Live Preview

Step 1: HTML Code
HTML code is very simple. You have a layer with ID mymenu which is your menu container, section header (H3 tag) and section content (ul list) which contains some sub-links for each section:



... so, HTML code is something like this:

<div id="mymenu">
<h3 class="toggler menusection">Menu Section 1</h3>
<div class="element menusection">
<ul>
<li><a href="1">Link 1</a></li>
<li><a href="2">Link 2</a></li>
<li><a href="3">Link 3</a></li>
<li><a href="4">Link 4</a></li>
</ul>
</div>

<h3 class="toggler menusection">Menu Section 2</h3>
<div class="element menusection">
<ul>
<li><a href="1">Link 1</a></li>
<li><a href="2">Link 2</a></li>
</ul>
</div>
<!-- Add here other sections... -->
</div>

You can add other sections using the previous structure for each new section.

Step 2: Javascript Code
Ok, now add a link to mootools framework in the tag of your page using this line of code:

<script type="text/javascript" src="mootools.svn.js"></script>

...and add this code in the <head> tag of the page:

<script type="text/javascript">
window.addEvent('domready', function(){
var accordion = new Accordion('h3.menusection', 'div.menusection', {
opacity: false,
onActive: function(toggler, element){
toggler.setStyle('color', '#333333');
toggler.setStyle('background', '#FFFFCC');
},
onBackground: function(toggler, element){
toggler.setStyle('color', '#FFFFFF');
toggler.setStyle('background', '#999999');
}
}, $('mymenu'));
});
</script>


Step 3: Customize CSS code
You can change the look of your menu simply changing some lines of CSS code:




It's all! Download source code to reuse it in your projects.

Download this tutorial

blog comments powered by Disqus
Anonymous said...

Thanks, nice effect

Anonymous said...

Nice and simple tutorial, I love the accordion!
Anyway I think you should use the cursor:pointer; property to make people aware that they're actually clicking a button to activate the slider.

Ciao antonio!

Social Network Web Design said...

Thanks for the tutorial.. Cool effect!

Anonymous said...

Hello,
i really like this effect for menu. I did all customization and now fits my design, but theres one problem.

How i can use active state for the links, so the menu will keep activated link that i clicked on.

Thanks!

Rade

jackpot said...

This is a nice blog. I like it!

Anonymous said...

i downloaded the src but in ie it is showing the activeX warning,
In firefox it works fine.

Any idea???

Anonymous said...

Ye.. nice blog. :| nothing support...

Tommie Hansen said...

Uhm, why use those extra divs instead of UL UL? Like: (NOTE: All < and > replaced with | due to Bloggers filtering ..)

|ul|

|li class="toggler"|Toggle 1|/li|
|ul class="toggledcontent"|
|li|Item 1|/li|
|li|Item 2|/li|
|li|Item 3|/li|
|/ul|
|li class="toggler"|Toggle 2|/li|
|ul class="toggledcontent"|
|li|Item 1|/li|
|li|Item 2|/li|
|li|Item 3|/li|
|/ul|

|/ul|

etc..? :f

anythinggoogle said...

Is there some way of having the second (or third) section active/expanded by default?

MySpace Design said...

So helpful!! Thank you!

helloworld said...

hi antonio.

may i ask, is it possible to use this accordion technique with tables?

say if i click a <tr> element, the details of that record will show below it.

thanks in advance.

NYC Web Designers said...

I modified your code to do something slightly different and screwed up.
Still testing

Joey said...

Hello

Very nice, I have this implemented but am wondering, how do I make a hover state for the togglers? I tried doing it with the css but it didnt work.

is there a command simliar to

onActive or onBackground that it would be, like onHover?

many thanks, Joey

  • Twitter Follow woork on Twitter
  • RSS Feed Subscribe to stay up to date
  • Podcast Coming soon...
  • 0 delicious lovers save
Share your links. Do you want to suggest any interesting link about web design or tech news? Submit your link.
Submit a News