jQuery Edge: Simplified .hover()
Thursday, May 28, 2009
In jQuery 1.3.3 1.4 the .hover() method will optionally accept a single method instead of always requiring two methods. This allows you to contain your logic within one method instead of duplicating it among two. I don’t know about you but I’ve avoided using the .hover() method in favor of just manually binding the mouseenter and mouseleave events so that I could use a single function to handle both events. So, lets look at a typical use-case for hover.
$('li')
.hover(function(event) {
$(this).addClass('test');
}, function(event) {
$(this).removeClass('test');
});
Now, this is how I’d typically handle this without the use of .hover().
$('li')
.bind('mouseenter mouseleave', function(event) {
$(this).toggleClass('test');
});
Now lets use the new functionality of .hover() instead of manually binding the events.
$('li')
.hover(function(event) {
$(this).toggleClass('test');
});
Sweet!
Sometimes though you’ll need to do more complex interactions than just toggling a class. In these cases you can simply check the event type that is passed to your handler. When the user mouses over, the event type sent to the hover function will be mouseenter. When the user mouses out, the event type sent to the hover function will be mouseleave. Here is an example checking the event type.
$('li')
.hover(function(event) {
$(this)[ (event.type == 'mouseenter') ? 'add' : 'remove') + 'Class' ]('test');
});
In the above example I’m still just toggling the class on the element that was hovered over/out but doing so in a more verbose way to illustrate how you can check the event type.
Posted in jQuery, jQuery Edge with 7 comments
This is a great idea Brandon!
By Peter Higgins on Friday, May 29, 2009 at 09:14 PM
Worth noting that these will be equivalent:
It’s a nice bit of sugar for power users, but I’m sure beginners will use this shorthand unintentionally.
With some massaging the docs should help to clarify this. Great example btw.
By Paul Irish on Friday, May 29, 2009 at 09:18 PM
@Peter, Thanks but it isn’t really just my idea. Several enhancement tickets have been filed for this particular feature. I believe you filed one as well. :)
By Brandon Aaron on Friday, May 29, 2009 at 10:19 PM
I always love reading your blogs posts and finding out what is coming in the next version of jQuery! Thanks Brandon… I do wish your posts were avilable via rss feed though.
By Josh Powell on Saturday, May 30, 2009 at 06:06 PM
@Josh, Thanks! There is an RSS feed. Your browser should let you know about it. Maybe I should post a link to it though. :)
Here it is: http://feeds.feedburner.com/BrandonAaron
By Brandon Aaron on Saturday, May 30, 2009 at 06:44 PM
Really nice way to shwo an hover-effect to all the element I need, thank you very much for sharing it!
By Webstandard-Blog on Wednesday, April 21, 2010 at 09:00 AM
Really nice way to show the hover-effect to all the element I need, thank you very much for sharing it! Greetz from Germany
By boxfresh on Thursday, May 6, 2010 at 07:05 PM