Implement jQuery’s hover() Method in MooTools
jQuery offers a quick event shortcut method called hover that accepts two functions that represent mouseover and mouseout actions. Here's how to implement that for MooTools Elements.
The MooTools JavaScript
/* hover! */
Element.implement({
'hover': function(fn1,fn2) {
return this.addEvents({
'mouseenter': function(e) {
fn1.attempt(e,this);
},
'mouseleave': function(e) {
fn2.attempt(e,this);
}
})
}
});
We implement hover() which accepts to functions; one will be called on mouseenter and the other on the mouseleave event. Each function is passed the corresponding event.
The Usage
/* fade in and out on hover event */
$('hover-me').hover(function(e) {
this.fade('out');
}, function(e) {
this.fade('in');
});
This simple example usage makes use of element fading.
![Create a CSS Flipping Animation]()
CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...
![Create a Sheen Logo Effect with CSS]()
I was inspired when I first saw Addy Osmani's original ShineTime blog post. The hover sheen effect is simple but awesome. When I started my blog redesign, I really wanted to use a sheen effect with my logo. Using two HTML elements and...
![Creating Spacers with Flexbox]()
I was one of the biggest fans of flexbox before it hit but, due to being shuffled around at Mozilla, I never had the chance to use it in any practice project; thus, flexbox still seems like a bit of a mystery to me. This greatly...
![9 Incredible CodePen Demos]()
CodePen is a treasure trove of incredible demos harnessing the power of client side languages. The client side is always limited by what browsers provide us but the creativity and cleverness of developers always pushes the boundaries of what we think the front end can do. Thanks to CSS...
Sorry Bro, but this is completely useless. There is no need for a “hover”-method with a sloppy API like jQuery has (there is clearly no point in “argument one is hover event and argument two is the mouseout event”).
After all, the mootools approach isn’t really significantly more to write but it gives you more flexibility and more meaningful code:
Also, by doing it the “mootoolsian”-way you can remove the events if you want or need to by doing
el.removeEvent('mouseenter', fn)
.@Chris
I like this, but after reading your reply I do agree with you. The reason why I like it is that it shows how flexible MooTools is — There is [almost] nothing that MooTools cannot do that other libs can. The thing that makes hover() less MooTools and more jQuery is the autobinding.
Great post though.
MooTools automatically binds every function to the element unless the function is bound already.
Consider this example:
The first example will log the element and the second one will be bound to “someOtherElement”. We have it all!
I see your point guys — I created this because it simply gives the dev a shorter way to accomplish something. It may also help jQuery users who are moving to Moo by making things a little bit more simple.
Chris, that would be true if David’s code was written as it were in your first post, but the function with the action is enclosed inside of the anonymous (already bound) function.
This looks kinda familiar :) http://code.google.com/p/mooj/source/browse/trunk/mooj.js#83
Damn Lim, I had forgotten about Mooj. You need to do a series of articles on that thing.
Chris, you can do the same in jQuery also, you don’t have to use
hover()
.As an exercise in demonstrating how easy it is to add your own syntax to MooTools, I think this post is totally fine and appropriate. None of us would ever do this for all the reasons pointed out in the comments above.
My only comment is that it should return
this
for chaining:So I have to throw in my $0.02US —
I implement this for Dojo based on the same “wow, why do people get so excited about an API that already exists” … but I changed the
.hover()
API some:allowing you to do one function for both states:
Though I put it in my “mini lib” plugd:
http://code.google.com/p/plugd/
I even submitted a patch to jquery because I think the single function use makes sense and is even more ‘rockstar’ than two inline functions:
http://dev.jquery.com/ticket/3498
Regards,
Peter Higgins
It has potential! Got some idea, will share when finnished.
Hello. I’am looking for this jQuery-Function in Mootools. Any Idea?
@joe This should do what you ask:
@Constantin:
Which can be backported to jQuery
It’s not because jQuery has shortcuts that you must use them.