Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

jQuery Link Nudging

40 Responses »

A few weeks back I wrote an article about MooTools Link Nudging, which is essentially a classy, subtle link animation achieved by adding left padding on mouseover and removing it on mouseout. Here's how to do it using jQuery:

The jQuery JavaScript

$(document).ready(function() {
	$('a.nudge').hover(function() { //mouse in
		$(this).animate({ paddingLeft: '20px' }, 400);
	}, function() { //mouse out
		$(this).animate({ paddingLeft: 0 }, 400);
	});
});

It's important to keep the nudge small and quick, so I set the animation to 20 pixels over 400 milliseconds.

Discussion

  1. November 28, 2008 @ 10:54 am

    Uhm… If you move your mouse quickly trough all items, you will have an annoying animation.
    i think you should add stop() before animate() so the code will be this:

    $(this).stop().animate({ paddingLeft: ’20px’ },400);

    Anyhow, nice example :P

  2. November 28, 2008 @ 11:51 am

    Yep, nicely done Dave, although Staicu’s method is a definite necessity.

    Another thing to ponder is, if your anchors have a fixed width, when animated the width will be reduced which can sometimes make the text wrap.

    To avoid this, it helps to have padding already setup on the anchors. For example if I had anchors setup with 5px left and right padding, this would work:

    $(‘a’).hover(function(){
    $(this).stop().animate({paddingLeft:’10px’,paddingRight:0},100);
    },function(){
    $(this).stop().animate({paddingLeft:’5px’,paddingRight:’5px’},100);
    });

  3. al
    November 28, 2008 @ 12:03 pm

    Nope. Feels more like a flash game than an useful addition to a site.

  4. November 28, 2008 @ 12:11 pm

    You’re been voted!!
    Track back from http://webdevvote.com/Javascript/jQuery_Link_Nudging

  5. November 28, 2008 @ 12:47 pm

    I had .stop(); on mine…. but… you really don’t get the fun wave effect when you use stop. You generally mouse over those links so fast that you barely see them budge at all. Without .stop();, it’s a neater effect, but then you run into the problem of the animation running over and over and over when you mouse on and off of a link quickly. I wonder if there is a good in-between.

  6. November 28, 2008 @ 12:55 pm

    @Staicu Ionut: I’m with Chris on this — I feel as though “the wave” enhances the overall effect.

    @James: Great tip, but I hesitate to put that into my script because I feel the padding adjustments should be a judgment call. You do have an excellent point though.

    @Al: Ummmm…OK.

  7. November 28, 2008 @ 3:10 pm

    @Al: O_o I cannot relate flash games that a link effect to “sugarize” a navigation style…

    @Chris: Precisely!

    @David: The jQuery animation is smoother than the MooTools’s you’re using on your site and showed in the previous post, I wonder why :/

  8. November 30, 2008 @ 3:05 am

    You’re been voted!
    Track back from http://webdevvote.com/Javascript/jQuery_Link_Nudging

  9. November 30, 2008 @ 5:39 am

    For my two cents worth, I think the animation is just perfect as it is. I, too, enjoy the wave-effect if you mouse over them all quickly. It’s subtle. It certainly is not like a casino or something.

    @Al …. come on, buddy. Ya gotta lighten up a bit. What do you define as a “useful addition”? I moused over them all real fast, thought the wave looked neat, I chuckled a bit and said “oh, I like that”, and therefore to me it was a quite useful addition!

    I’d leave it just like it is. A blinking yellow background? No. A dragon head popping out of every link on the page? No. A subtle little movement of the link – yes. Fine job.

  10. November 30, 2008 @ 8:36 pm

    This is great Dave!

    I’m trying it out on my blog in the titles of my posts. I know it probably feels better when there is a list of links (for the wave effect) but I like the extra touch in my titles.

    Thanks for this,

    Patrick

  11. December 5, 2008 @ 3:46 pm

    Pretty nice, clean effect. I have one useful tweek. Some may not want to return to paddingLeft 0 or expand to paddingLeft 20px based on the initial padding, so you might use +=20. (I just recently found out about this feature and it’s quite nice!

    $(‘a.nudge’).hover(function() { //mouse in
    $(this).animate({ paddingLeft: ‘+=20′ }, 400);
    }, function() { //mouse out
    $(this).animate({ paddingLeft: ‘-=20′ }, 400);
    });

  12. December 5, 2008 @ 4:29 pm

    Sorry, I got carried away with your idea and added another line or two in there so it feels more like jQuery..

    jQuery.fn.nudge = function(o){
    o = jQuery.extend({},{ indent:20, speed:400 },o);

    jQuery(this).hover(function() { //mouse in
    jQuery(this).animate({ paddingLeft: ‘+=’+o.indent }, o.speed);
    }, function() { //mouse out
    jQuery(this).animate({ paddingLeft: ‘-=’+o.indent }, o.speed);
    });
    }

    So we can call it like:

    $(‘a.nudge’).nudge();

    Or change some settings:

    $(‘a.nudge’).nudge({indent:20,speed:400});

  13. December 5, 2008 @ 4:33 pm

    I thought I’d add another line or two to make it feel more like jQuery:

    jQuery.fn.nudge = function(o){
    o = jQuery.extend({},{ indent:20, speed:400},o);

    jQuery(this).hover(function() { //mouse in
    jQuery(this).animate({ paddingLeft: ‘+=’+o.indent }, o.speed);
    }, function() { //mouse out
    jQuery(this).animate({ paddingLeft: ‘-=’+o.indent }, o.speed);
    });

    }

    so you can call it like:

    $(‘a.nudge’).nudge();

    or add some options:

    $(‘a.nudge’).nudge({ indent:40, speed:300 });

  14. jon
    December 13, 2008 @ 5:00 pm

    Great tutorial!!

    One question. If I have a horizontal list and wanted to animate this in a similar fashion to your tutorial (except I want to animate the links to go up and down), would it just be a matter of changing the values to bottomPadding…etc?? To adapt for a horizontal menu animating up and down?

    Thanks in advance for any guidance. Keep up the great work!

    ~Jon

  15. December 28, 2008 @ 8:11 pm

    awsome stuff this lil technique. Yeah it is like a flash application. I’ve adapted this into some experimental CSS apps im playinf with.

    It makes me wonder if jQuery can proform these animated menus, what other animated effects it can acheive?

    If any one has any examples please post. I’d love to hear from ya.

    Good s%#t!

  16. December 29, 2008 @ 2:24 pm

    Really awesome!

    Thanks for sharing.

  17. jared
    January 3, 2009 @ 5:47 pm

    @Jared – You are able to manipulate all kinds of css attributes such as padding, margin border widths etc.

  18. dubbelj
    January 23, 2009 @ 10:05 am

    This is pretty much the first thing you’ll learn while just trying jQuery out… Why dedicate an article to such a simple function with hardly any practical uses?

  19. January 30, 2009 @ 5:48 am

    …because everyone has to start somewhere? Also, if you’ll notice, it’s the jQuery version of another article he wrote which featured the same effect but with MooTools; makes perfect sense to me.

    Also, just because you can’t think of a practical use doesn’t mean there isn’t one. Seriously, what’s with all the nay-sayers? Nobody’s forcing you to use the technique or to read the article; if you don’t care for it, just move on along.

  20. March 9, 2009 @ 3:08 am

    A practical use would be that of the title… nudging links. It’s great for a list of links because it actually increases the readability of link list or…. navigation link lists, which is exactly what I use this for on several sites. I first found Chris Coyier using this and implemented his technique.

    Nice demo article. I like the wide range of topics discussed.

  21. March 23, 2009 @ 10:57 am

    thanks for your tutor. I use the blogspot, and I change the ‘padding’ to ‘opacity’.

  22. April 22, 2009 @ 2:26 pm

    I’m very new to Javascript. I can’t get this to work. I have the jQuery library uploaded and the snippet of code you posted uploaded too.

    I set up a demo page just to see how it all works, and I have made it degrade gracefully with CSS that does a similar job. Here’s the link to my demo page: Link Nudging Effect | Vizion3. Here’s the link to the jquery.js file.

    The link_nudge.js file contains:

    $(document).ready(function() {
    $(‘#nav li a:hover’).hover(function() {
    $(this).animate({ paddingLeft: ’24px’ }, 400);
    }, function() {
    $(this).animate({ paddingLeft: ’12px’ }, 400);
    });
    });

    Please can you help me to get it to work?

    Thanks. :)

  23. April 23, 2009 @ 2:25 pm

    I’ve got it to work now – it seems the link_nudge.js file I uploaded to the server was corrupted. So you can ignore my last comment. I do feel that the MooTools version is better. So I’ll probably end up using that version.

    Thank you for your tutorial. :-)

  24. mike
    April 30, 2009 @ 8:13 pm

    I am using this in a div that has a floated div next to it, and the nudging is moving the floated div, is there a way to avoid this?

  25. April 30, 2009 @ 8:17 pm

    @Mike: Set a width for the anchor’s containing DIV.

  26. mike
    April 30, 2009 @ 8:37 pm

    It has a width set, I am wondering if perhaps the width just isn’t wide enough to also accommodate the nudge?

  27. john
    August 6, 2009 @ 10:46 am

    Is it possible to have the text actually nudge upwards instead of left or right? If so, how would one go about doing this?
    Thanks in advance!

  28. September 22, 2009 @ 9:27 am

    hi!

    i really like this plugin. thanks for it!

    i’m having an issue on a little site i did for a friend’s movie

    http://wanderlostfilm.com/

    it works smashing on every page but my post detail pages

    http://wanderlostfilm.com/wanderlost-invited-to-attend-independent-film-week-in-nyc-trailer-unveiling/

    I get the js error in firebug

    $(“.page_item a”) is null
    http://wanderlostfilm.com/wp-content/themes/wanderlost/js/jquery.nudge.js
    Line 39

    please let me know if you have any advice. THANKS

  29. andrew s.
    November 9, 2009 @ 3:28 pm

    David, is there any way to make by Blogger side items nudge like this?

    BTW, your blog is awesome! Thanks for sharing. :)

    - @

  30. drew
    November 18, 2009 @ 5:50 pm

    This is nice, can someone explain how to implement this onto a site? I see the code but don’t know what to do with it. Thanks! I use iweb thanks in advance…

  31. andrew s.
    November 18, 2009 @ 6:08 pm

    @Drew

    I’m not so great at this Javascript, but this is how I got it to work:

    Download jQuery from http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.min.js

    Place it in a folder on your site, i.e. http://www.yoursite.com/jquery/

    Create a new file in that folder, i.e. scripts.js

    In the blank file type the following: // David Walsh Link Nudging

    Then, on the next line, insert the code that David provides in this article.

    Now, go to your index page, and before the “” place the following lines:

    Now, for every link you want to use this affect, within the link tag type class=”nudge”. For example, if the link looks like this:

    Home

    change it to

    Home

    And, that should be it. Of course, there are a bunch of variables that you may have to account for that may be specific to your site, but that’s the basics of it.

    Hope this helped! :)

    - @

  32. andrew s.
    November 18, 2009 @ 6:11 pm

    @Drew

    Sorry, unfortunately, I dunno how to make the code lines appear here.

    - @

  33. drew
    November 18, 2009 @ 10:04 pm

    Hi, thanks :)

    Ah, can anyone fill in the blanks for me? Thanks :)

  34. andrew s.
    November 18, 2009 @ 10:57 pm

    @Drew,

    Ok, I put it up here for you: http://fotofoto.ca/jquery-link-nudge.html

    - @

  35. February 3, 2010 @ 9:47 pm

    Great article. Took me a minute to get it to work. I didn’t realize you had to give any link you want this to apply to a class of “nudge.” After I did that it worked like a charm. Thanks!

    Tim

  36. majid
    February 10, 2010 @ 7:30 am

    @Staicu Ionut: yes this is good trick .

  37. majid
    February 10, 2010 @ 7:30 am

    @Staicu Ionut: yes this is good trick .

  38. March 4, 2010 @ 11:08 am

    It’s really nice sharing :)

  39. razvan
    March 18, 2010 @ 6:57 am

    how can I move ths link verticaly?

  40. August 17, 2010 @ 3:49 pm

    Trent: What a wonderful tweak! .. “+=20″ This really did the trick.. I’ve been working on this kinda effect in a vertical menu.. and the same thing happende level 2 li links returned to: padding-left: 0; and that really sucked.. Your way is more dynamic..

    One problem though: .. when using .stop().animate(….the problem is back.. after hover: padding-left: 0; once again..

    Awsome blog, awsome users.. :) I tend to learn more from comments than from any of jQuerys tuts..

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!