Link Nudge jQuery Plugin

Wednesday, August 5th, 2009

I think it was David Walsh who first showed me the idea of link nudging using jQuery. You can see on the sidebar we also use this technique. I haven't seen a plugin made to do this yet (as it is really simple) so I figured this would be perfect for a quick jQuery plugin tutorial. You can download the plugin and/or view the demo below.

View Demo

Authors Note:This plugin has now been given the name of NudgeIt and has had a lot of new features added! Please see the two updates at the bottom of this page for more information.

The Plugin Javascript

Creating the plugin is simple and is as easy as creating any other jQuery plugin. I'll try to comment the source code some so you can get an idea of how it all works.

jQuery Link Nudge Plugin
Posted by DrewDouglass on August 5th, 2009
Plugin Source.

(function($){
               
                //Define the plugin, named 'linkNudge'
                $.fn.linkNudge = function(params){
                       
                        //set default parameters
                        params = $.extend({
                                padding: 20,
                                elapseTime: 300,
                                returnPadding: 0}, params);
                       
                        //Traverse every node passed
                        this.each(function(){
                               
                                //Define this as a single object
                                var $t = $(this);
                               
                                //Proceed to nudge on hover
                                $t.hover(function(){
                                        $t.stop().css('cursor', 'pointer').animate({paddingLeft: params.padding}, params.elapseTime);   
                                }, function(){
                                        $t.stop().animate({paddingLeft: params.returnPadding}, params.elapseTime);
                                });
                               
                        });
                       
                        return this;//Allow for chaining.
                };
       
        })(jQuery);

Usage

$(function(){
		$('ul li:even').linkNudge({
			elapseTime: 500
		}).css('list-style-type', 'circle');//Plugin allows for chaining, as seen with the css call.
	});

Documentation

To use the plugin, simply include the plugin and jQuery library on your page and make a call to the linkNudge function. You can include three optional parameters, as discussed below.

  1. padding - Allows you to set how far the link should be nudged. Defaults to 20px.
  2. elapseTime - The time, in milliseconds, over which the nudge should occur. Default to 300.
  3. returnPadding - Optional padding to return to when the element is no longer being hovered. Defaults to 0.
  4. Terms of Use

    You can't sell it. Otherwise, use it how you would like in your projects, a credit to this page in the notes or documentation would be appreciated. Donations even more so.

    View Demo

    Feed Me

    If you have used or enjoy this plugin, I urge you to consider making a small donation so I can buy some Chinese food.


    Update:

    David Walsh has modified the flexibility of the plugin in a new post on his blog. I encourage all of you to give it a look and download and see how they differ. You can find Davids plugin on his website. Thanks for adding on to and improving the plugin David!

    Update Two!

    David and I have decided to collaborate on this plugin and host it on a GitHub so everyone can easily keep track of any updates or changes. We are now also calling the plugin, quite appropriately, NudgeIt.


    If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Hi, I'm Drew Douglass and I make sure Dev-Tips.com runs smoothly. I also work for Envato Support and write for NETTUTS.com and ThemeForest.net. I'm passionate about anything web development related, especially php, WordPress, MySQL, and jQuery. Feel free to follow me on twitter.
  1. August 5, 2009 at 2:08 pm
    • August 6, 2009 at 4:25 pm
  2. August 6, 2009 at 1:25 pm
  3. August 6, 2009 at 7:35 pm
    • August 7, 2009 at 12:00 am
  4. August 10, 2009 at 11:37 pm
    • August 11, 2009 at 12:12 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates