Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

MooTools Accordion: Mouseover Style

28 Responses »

Everyone loves the MooTools Accordion plugin but I get a lot of requests from readers asking me how to make each accordion item open when the user hovers over the item instead of making the user click. You have two options: hack the original plugin or simply fire the click event when the mouse enters the toggler. My vote is for option two -- here's how to do it.

The MooTools JavaScript

//make the accordion
var accordion = new Accordion($$('.toggler'),$$('.element'), {
	opacity: 0,
	onActive: function(toggler) { toggler.setStyle('color', '#f30'); },
	onBackground: function(toggler) { toggler.setStyle('color', '#000'); }
});

//make it open on hover
$$('.toggler').addEvent('mouseenter', function() { this.fireEvent('click'); });

As easy as it gets -- simply fire the click event when the user's mouse enters the toggler!

If you aren't familiar with the MooTools Accordion, click here.

Discussion

  1. September 4, 2008 @ 12:05 pm

    Thanks David.

    I was just contemplating the mooTools accordian for one of my projects at hand.
    You’ve out-slicked yourself with this offering. Thanks for this post.

    Michael

  2. September 4, 2008 @ 5:06 pm

    Thanks, David.

    This sure is useful :)

  3. simon
    October 1, 2008 @ 8:07 am

    I have been trying to get this to work for weeks now!
    When I finally typed in the magic right text to Google up this page – it worked in seconds!
    Many thanks
    I wish it had been easier to find though, I have ben on this site for ages and missed it…
    ..perhaps it wasn’t there yet though!

    Brilliant.. Phew.

    now I have the next problem to fix!

  4. October 27, 2008 @ 7:20 pm

    Hey there!

    i’ve just got my head around mootools (i’m a php developer, not a miracle worker, jim) and i was wondering if anyone had tried tinkering with the transition/tween fx to get the bounce effect on the open/close tabs.

    also if anyone could develop a real-life ctl+z so i can undo the above obscure star trek reference, that’d be great.

    kthxbai

  5. black
    November 5, 2008 @ 8:19 pm

    Thanks a lot !!
    I was working for a few days on this accordion without finding any solution !

    Now it’s done !

    Thanks again =)

  6. zaw thet
    February 4, 2009 @ 7:30 am

    hello david !
    how can i do mootools accordion for style.
    my code used Accordion.
    don’t use fx.accordion.

  7. tim
    February 23, 2009 @ 10:38 am

    Nice trick, I was about to hack the source code when I ran across this. Thanks.

  8. February 24, 2009 @ 6:56 pm

    Thanks for the tip. Any way of making it close automatically once the mouse moves away from the toggler?

  9. March 26, 2009 @ 2:18 pm

    Hi David, I’m trying to implement submenus within the accordion from your script. Whenever I click a submenu item my css is made to highlight that particular li and also go to that page. But each li in the same position in every section of the 3 accordion sections highlights. For example, if I click the first li link, it highlights and so does the first link in the second accordion tab and also the first link in the third accordion section. How would I go about isolating only the particular li link I’m interested in? Thanks ahead of time!

    print(”

    <script type=”text/javascript”>
    // uses mootools.js
    window.addEvent(‘domready’, function() {

    //make the accordion
    var accordion = new Accordion($$(‘.toggler’),$$(‘.element’), {
    opacity: 0,
    display: “1″, // set to “” for all closed
    onActive: function(toggler) {
    toggler.setStyle(‘color’, ‘#cccccc’);
    },
    onBackground: function(toggler) {
    toggler.setStyle(‘color’, ‘#6c6c6c’);
    }
    });

    //make it open on hover
    $$(‘.toggler’).addEvent(‘mouseenter’, function() { this.fireEvent(‘click’); });

    });
    </script>

    <div id=”wrapper_accordion”>
    <div id=”accordion”>

    <h3 class=”toggler”><a href=”corpSupport.html”>CORPORATE SUPPORT</a></h3>
    <div class=”element”>
    <ul class=”accordion_menu_sub”>
    <li><a href=”corpSupport_a.html” class=”subpg_a” >CSR Working Group</a></li>
    <li><a href=”corpSupport_b.html” class=”subpg_b” >Governance</a></li>
    <li><a href=”corpSupport_c.html” class=”subpg_c” >SRI Ratings</a></li>
    <li><a href=”corpSupport_d.html” class=”subpg_d” >Government Relations</a></li>
    </ul>
    </div>

    <h3 class=”toggler”><a href=”ssFoundation.html”>FOUNDATION</a></h3>
    <div class=”element”>
    <ul class=”accordion_menu_sub”>
    <li><a href=”ssFoundation_a.html” class=”subpg_a” >ssFoundation_a</a></li>
    <li><a href=”ssFoundation_b.html” class=”subpg_b” >ssFoundation_b</a></li>
    <li><a href=”ssFoundation_c.html” class=”subpg_c” >ssFoundation_c</a></li>
    </ul>
    </div>

    <h3 class=”toggler”><a href=”volunteerism.html”>VOLUNTEERISM</a></h3>
    <div class=”element”>
    <ul class=”accordion_menu_sub”>
    <li><a href=”volunteerism_a.html” class=”subpg_a” >Volunteerism_a</a></li>
    <li><a href=”volunteerism_b.html” class=”subpg_b” >Volunteerism_b</a></li>
    <li><a href=”volunteerism_c.html” class=”subpg_c” >Volunteerism_c</a></li>
    </ul>
    </div>

    </div>
    </div>”);

  10. joe
    April 2, 2009 @ 2:03 pm

    I figured it out. It’s not a js issue, it was a css issue. For those who want to know: I just forgot to make the ‘a class’ names truly unique. For example, by having more than one ‘class=”subpg_c” it highlight all these ‘li’ tags in 3 places at once. Sorry for the long post. I thought the code might show up in a separate window rather than in the post. ;) keep up the great site!

  11. April 3, 2009 @ 10:41 am

    Thanks! Not being a programmer your tutorials have helped me use mootools and add excitement to my Web site. Is there a way to set the height. I’m doing something similar to what Apple did with scriptaculous. Thanks again, you’ve been a great help.

    Dave

  12. shawn
    April 23, 2009 @ 9:14 am

    This Accordion looks awesome. I am trying to use it for a project and am having a little difficulties getting it to work. Currently all the data shows on the page. None is collapsed and so on. Here is what I am using:

    <style type=”text/css”>
    .toggler { cursor:pointer; font-weight:bold; color:#000; margin: 0; padding:5px; font-size:16px; font-family:Arial, sans-serif; }
    .toggler:hover { background:#eee; }
    .element { padding:0 5px; }
    .image { margin:10px 20px; }
    </style>

    <script type=”text/javascript”>
    //make the accordion
    var accordion = new Accordion($$(‘.toggler’),$$(‘.element’), {
    opacity: 0,
    onActive: function(toggler) { toggler.setStyle(‘color’, ‘#f30′); },
    onBackground: function(toggler) { toggler.setStyle(‘color’, ‘#000′); }
    });

    //make it open on hover
    $$(‘.toggler’).addEvent(‘mouseenter’, function() { this.fireEvent(‘click’); });
    </script>

    That is in the head of the document.

    This is in the body:

    <div id=”accordion”>
    <h3 class=”toggler”>Unit 1</h3>
    <div class=”element”>
    <p>Unit 1 HTML goes here</p>
    </div>
    <h3 class=”toggler”>Unit 2</h3>
    <div class=”element”>
    <p>Unit 2 HTML goes here</p>
    </div>
    <h3 class=”toggler”>Unit 3</h3>
    <div class=”element”>
    <p>Unit 3 HTML goes here</p>
    </div>
    <h3 class=”toggler”>Unit 4</h3>
    <div class=”element”>
    <p>Unit 4 HTML goes here</p>
    </div>
    </div>

    Thanks in advance for the help.

  13. sascha
    May 11, 2009 @ 8:30 am

    Hy!

    Is it possible to close a tab after the mouse leaves?
    Sorry for this stupid question, but i am absolutly new with this stuff!

    Thanks for yor help!

  14. July 8, 2009 @ 8:24 pm

    I had one heck of a time getting the accordion to work where if you click it, it opens, if you click it again, it closes, if your mouse leaves the element, it closes.

    <div class=”toggler”>Click Me
    <div class=”element>
    <ul>
    <li>Showing</li>
    <li>Showing Again</li>
    </ul>
    </div>
    </div>

    window.addEvent(‘domready’, function() {
    var accordion = new Accordion($$(‘.toggler’),$$(‘.element’), {
    display: -1,
    opacity: 0,
    openClose: true,
    alwaysHide: true
    });
    });
    $$(‘.element’).addEvent(‘mouseleave’, function(){ this.parentNode.fireEvent(‘click’); });

  15. July 24, 2009 @ 4:14 pm

    Thank you for the mouseover code on the accordian. I feel this is slightly easier and more intuitive than the click for the general public to use on the website. This has saved me a huge amount of work and will improve the look of the site. Wish I could have written it!

  16. September 17, 2009 @ 6:15 am

    $$(‘.toggler’).addEvent(‘mouseenter’, function() { this.fireEvent(‘click’); });

    Good work on that

  17. October 12, 2009 @ 9:56 am

    Hi!

    I am trying to make mootools good working with IE using you’re example for first…

    but the result is http://www.voilagesdesign.fr/test

    I use the latest realease of mootools but I’d try first with 1.2 and the result is the same…

    any idea?

    thanks for your website david, it is very enriching!

  18. December 3, 2009 @ 9:57 am

    Perhaps you should add a notice that there is a accordion option called “trigger” which should do exactly what is done with your workaround. Sadly mootools implemented options which doesnt take effect.

    The more im working with mootools the more i miss jQuery. Easier to use and more docs / demos / plugins.

  19. luis
    December 14, 2009 @ 8:14 pm

    i couldn’t find it even in mootools. You’re the man, thank’s a lot!!!

  20. john
    December 16, 2009 @ 4:26 pm

    It’s wonderful the trick but i have a question, how can i link it in an iframe?

    I have my accordion in a main html and i tried this: but doesn’t work.

    I would appreciate it if somebody helps me.

  21. john
    December 16, 2009 @ 4:32 pm

    Sorry, it get the code. I mean tried this:

    [code][/code] but doesn’t work.

  22. john
    December 16, 2009 @ 4:36 pm

    :s again get the code i really mean: a href=”europa0.html” target=”catalogos” with the

  23. March 11, 2010 @ 11:47 am

    awesome work , but .. can u give the script some option for bounce slide ? i.ve seen that this is posible , but im noob in mootools :P . pretty tnx , waiting for your reply .

  24. March 11, 2010 @ 11:52 am

    transition: Fx.Transitions.Bounce.easeOut,

    this is the question :P … for me worked clear !

  25. March 16, 2010 @ 11:16 am

    Hey there.

    I use the mootools accordion for my website. But i just want to add a simple property. I’d like to have an accordion that opens 1 or 2 seconds after opens the website. How do i realize that? I am not familiar with Javascript, so need help!

    Greets.

  26. March 18, 2010 @ 6:50 am

    Very beatiful plugin thanks

  27. April 24, 2010 @ 5:08 am

    Cool site, i have bookmarked it for future reference

  28. joe
    June 13, 2010 @ 3:51 pm

    was easy to implement. great plugin.

    but.. I’m having trouble with .elements top and bottom borders. I can’t get them to display. The left and right borders show fine. Any advice?

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!