MooTools Accordion: Mouseover Style

By  on  

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.

Recent Features

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

Incredible Demos

  • By
    Reverse Element Order with CSS Flexbox

    CSS is becoming more and more powerful these days, almost to the point where the order of HTML elements output to the page no longer matters from a display standpoint -- CSS lets you do so much that almost any layout, large or small, is possible.  Semantics...

  • By
    FileReader API

    As broadband speed continues to get faster, the web continues to be more media-centric.  Sometimes that can be good (Netflix, other streaming services), sometimes that can be bad (wanting to read a news article but it has an accompanying useless video with it).  And every social service does...

Discussion

  1. 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. Thanks, David.

    This sure is useful :)

  3. Simon

    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. 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

    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

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

  7. Tim

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

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

  9. Joe

    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!

  10. 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

  11. Sascha

    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!

  12. 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'); });
    
  13. 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!

  14. $$('.toggler').addEvent('mouseenter', function() { this.fireEvent('click'); });

    Good work on that

  15. 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!

  16. 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.

    • yep, the “hover” event is called “mouseover” ;)

  17. Luis

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

  18. John

    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.

  19. John

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

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

  20. John

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

  21. 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 .

  22. transition: Fx.Transitions.Bounce.easeOut,

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

  23. 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.

  24. Very beatiful plugin thanks

  25. Cool site, i have bookmarked it for future reference

  26. Joe

    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?

  27. Oliver

    Looks great, does anyone know how to have it automatically close on mouse out? I’ll pay for someone to change the code so it closes on mouse out?

    • Flor

      I have the same question.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!