MooTools Link Nudging
Link nudging is the process of adjusting the padding on a link slightly to show a simple, tasteful "jump" when you place your mouse over a link. The effect is magnified when mousing on and off a link quickly. Here's the short snippet of code:
The MooTools 1.2
$$('#footer-topics-1 a').each(function(el) {
var fx = new Fx.Morph(el,{ duration:300, link:'cancel' });
el.addEvents({
'mouseenter': function() { fx.start({ 'padding-left': 20 }); },
'mouseleave': function() { fx.start({ 'padding-left': 0 }); }
});
});
Check out the links in my footer to see the effect. Short and simple, right?
Chris Coyier also uses this effect, and it just so happens we both implemented it a day apart. Great minds think alike?
I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener
and attachEvent
. Times have changed but there are still a few functions each developer should...
I love almost every part of being a tech blogger: learning, preaching, bantering, researching. The one part about blogging that I absolutely loathe: dealing with SPAM comments. For the past two years, my blog has registered 8,000+ SPAM comments per day. PER DAY. Bloating my database...
I've been creating a lot of slideshow posts lately. Why, you ask? Because they help me get chicks. A quick formula for you:
The following code snippet will show you how to create a simple slideshow with MooTools; the script will also...
Tabular data can oftentimes be boring, but it doesn't need to look that way! With a small MooTools class, I can make tabular data extremely easy to read by implementing "zebra" tables -- tables with alternating row background colors.
The CSS
The above CSS is extremely basic.
At first I was doing it with margin-left, which works but is problematic as the animation can slide the link out from under your mouse (creating a mouseOut), which then starts animating back the other way (creating a mouseOver), and you get the Jitter thing going on.
Also, more evidence that MooTools animation is smoother than jQuery.
yap, cool effect. i tried something like this on my page (main navigation “jumps” up) too months ago. http://www.bugeyes.de
I love it, thanks. Your blog never fails to produce, even with small stuff like this.
Crazy, simple and slim. i check it out!bThamnk you ^^
Works great, i did it ^^.
I’m curious, how would one do this in jQuery? In a site I’m working on I have jQuery stuff and I don’t want to have to have two libraries
Cant seem to get it working. Hmm. I wouldn’t think using Milkbox along side it would create a problem.
@Paul Davis: Can you provide a URL?
Cute. From a usability perspective I’d think having the text of a link remove itself from under your mouse would be considered sub-optimal and counter-intuitive.
I’ll adopt the idea, with the addition of something that enforces the notion that there’s still something under the cursor.
@ david
http://www.test.pauldavis.org.uk/tem/1/
@Dave Newton: My links are “display:block” so it’s not like clicking the link text’s original spot would cause a problem.
@Paul Davis: Your problem is caused by giving all of the list items the same ID, which you cannot do. Change ‘id=”navlink”‘ to ‘class=”navlink”‘ and you MooTools selector to “$$(‘.navlink’)”
@david: Ok, I’ve done that. Still no luck, do I need to apply some CSS .navlink string?
@Paul Davis: You went a little stray, but that’s ok. Change your Moo selector to:
Also, you need to add “domready” code:
@davis: Yay! It works! Thank you sir. =]
@David: I know, but it’s still disconcerting and non-obvious. Not horribly so, but enough so that I wouldn’t feel comfortable deploying it as-is.
Great tutorial. Thanks for share !
Nice effect, I just implemented it on my site! (jQuery’fied) :)
To avoid the text within the links wrapping I’ve applied a preliminary left and right padding (via CSS) and then I animate both of them (reducing the right padding and increasing the left padding at the same time) – this makes sure that there is the same amount of space for the text as there was before the animation, thus avoiding any confusing wrapping of text.
I’ve only done a 5px movement, I think I could get away with 10px before it starts looking weird.
What about…?
I’m not a JS expert, so this is something between a suggestion and a question…
In the original code:
new FX class
command is inside theeach
function, so i think (this is the part that i’m not so sure) the code creates a new “FX Class” instance with the same name -“fx”- for every element that the selector returns. So, maybe carrying the FX instance out of the each loop and than calling it inside the loop can increase the performance. (?)@Brane: That would give each link the same Fx. Wouldn’t that cause some issues?
@Brane: That wont work. You’d be “overwriting,” so to speak, each FX and it would only work on the last item.
@Caligula, @david:
Yeah, you’re right. I completely misinterpreted the FX.Morph class concept. (I blame being sleepy :))
Is there a possibility that it will linear slide out and bounce:out back? can get it working together. For the rest: GOOD WORK!!!
@gijs:
If I don’t misunderstand what you ask, it can be done like this:
@bran: thanks for the fast replay, unfortunately its to late for me, but i give it a try tomorrow. Is that the “normal” way to set up different transitions
for the same event?
@all: think that it will help for many people that they know you can use in stead of:
.start({ ‘padding-left’: 20 });
this is also possible:
.start({ ‘padding-left’: ’20’, ‘background-image’: ‘url(bla.jpg)’ });
so you can set more styles.
i discovered that ‘background’: ‘url(bla.jpg) no-repeat red’ doesnt work in IE7, you have to set all styles fully listed, no shortcuts.
Sorry for my english, i’m dutch
Greets,
Gijs
For the life of me I can’t this script to work!!!!
I’m a novice with mootools, and I thought I was getting better.. For the life of me, i can’t figure out why the nudging isn’t working….
link text
Can you help me? Check out the bottom of my page.
Many thanks.
Erik
drop your code here, and stop make it difficult to leave your site.
Don’t give up on me!! Your all I got!! :-)
What doesn’t work? The links in the #footer3 section are nudged, the ones that aren’t in a #footer3 section aren’t.
I got the nudge to work with jquery…. no luck with mootools.
Thanks.
Perhaps you should your SmoothScroll in the second
window.addevent
, do not use this twiceHey David,
Thanks for sharing all your awesome stuff, I have had no problems implementing everything you have shared so far even though I have 0 knowledge of javasript or mootools but this one isn’t as clear as your other examples at all. I cant get this to work. How do you make the a tags associate with this script and is there anything I have to put in the css?
Thanks for any help
The noob!!!
After upgrading to MooTools 1.3,
The link-nudge not always come back to original position
http://iluni-ftui.org/berita/
It does not always trigger
mouseleave
event.