Animating your dropdown menu
Published January 11th, 2010 under General
In our IE Hovers post we outlined how the Superfish jQuery plugin can be used to create a smooth flowing animated effect for your dropdown menu. However the Superfish script is reasonably large and many people don’t require the various options which are available with the Superfish plugin. So here is an explanation of a new way to create smooth animated dropdown menus with only a few lines of code (plus jQuery).
The following code will produce a clean smooth flowing animation effect just like you can see in the menu above for our own site.
[code language=”javascript”]
function suckerfishmenu(){
$(‘#suckerfishnav ul’).css({display:’none’}); // Opera Fix
$(‘#suckerfishnav li’).hover(function(){
$(this).find(‘ul:first’).css({display:’none’}).slideDown(300);
},function(){
$(this).find(‘ul:first’).css({display:’none’});
});
}
$(document).ready(function(){
suckerfishmenu();
});
[/code]
To make this work, you simply need to insert the above code after you load jQuery and between the <head> tags for your page and use a dropdown menu with an ID of #suckerfishnav. Make sure you reference the jQuery.js file correctly (available here). If you are using WordPress, then you will want to make sure you are loading jQuery via the enqueue method to avoid clashes with plugins which use jQuery and add var $ = jQuery; to ensure that jQuery works with the non-conflict mode of the version of jQuery version bundled with WordPress.
If all of that seems too complicated, don’t despair as the new version coming soon of our PixoPoint Menu plugin has this new animation method built in so that you won’t need to touch any code to get it working.
Kravvitz says:
Is there a particular reason why you’re using both the display and visibility properties?
Ryan, one might mention (for those of us who don’t use WordPress much) that the reason for setting “$” as an alias for “jQuery” in your own code is that WP calls “jQuery.noConflict()” so that Prototype can use “$”.
January 13, 2010 at 3:57 am # //
Ryan says:
Hmmm, good point. I hadn’t actually noticed I was both of them. I’ve been using that code for a while now and I guess when I fixed the Opera bug I just forgot that I was using both visibility and display. I’ll test it to make sure it works fine and change them to both display:none if that works.
Thanks for pointing it out 🙂
I’m somewhat of a JS idiot, so we when I get this stuff to work I usually don’t mess with it :p
January 14, 2010 at 5:46 am # //
Ryan says:
Thanks for pointing out that I only use var $ = jQuery; to make it work with WordPress. I might remove that line from the post actually to save confusing people.
I’m so used to working with WordPress that I just tack that on the front by habit now.
January 14, 2010 at 5:54 am # //
Ryan says:
I’ve removed the non-conflict line of code to save the confusion now.
January 14, 2010 at 5:59 am # //