Treehouse

Link Nudging Using Dojo

By on  
Dojo Toolkit

In the past we've tinkered with link nudging with MooTools and link nudging with jQuery. In an effort to familiarize myself with other JavaScript frameworks, we're going to try to duplicate that effect with another awesome framework: Dojo.

The JavaScript: Attempt 1

dojo.addOnLoad(function() {
	var links = dojo.query('a.nudge');
	//foreach...
	dojo.forEach(links,function(link) {
		var left = dojo.style(link,'paddingLeft');
		dojo.connect(link,'onmouseenter',function() {
			dojo.animateProperty({
				node:link,
				properties: {
					paddingLeft: (left + 10)
				}
			}).play();
		});
		dojo.connect(link,'onmouseleave',function() {
			dojo.animateProperty({
				node:link,
				properties: {
					paddingLeft: left
				}
			}).play();
		});
	});
});

Once the DOM is ready, we use the dojo.query method to find all of the links to nudge. For every link we find, we record its original left padding and add mouseenter and mouseleave events to each link to animate its left padding.

The JavaScript: Better Solution

dojo.ready(function() {
	dojo.query('a.nudge').forEach(function(link){
		var left = dojo.style(link,'paddingLeft');
		dojo.connect(link,'onmouseenter',function() {
		    dojo.anim(link, { paddingLeft:20 });
		});
		dojo.connect(link,'onmouseleave',function() {
		    dojo.anim(link, { paddingLeft:left });
		});
	});
});

Dojo lead Pete Higgins showed me a more condensed version of his script.

Simple, no? The best way to learn to use any JavaScript framework is to duplicate a given set of code you are familiar with, much like we did here. What do you think about this Dojo example? Look close to jQuery and MooTools?

ydkjs-4.png

Recent Features

  • 5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

  • Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • QuickBoxes for Dojo

    Adding to my mental portfolio is important to me. First came MooTools, then jQuery, and now Dojo. I speak often with Peter Higgins of Dojo fame and decided it was time to step into his world. I chose a simple but useful plugin,...

  • Build a Slick and Simple MooTools Accordion

    Last week I covered a smooth, subtle MooTools effect called Kwicks. Another great MooTools creation is the Accordion, which acts like...wait for it...an accordion! Now I've never been a huge Weird Al fan so this is as close to playing an accordion as...

Discussion

  1. The mouseenter/mouseleave thing is so common it is actually combined as an API in plugd (soon to be in Dojo proper). With plugd, the Dojo code could look like:

    $.ready(function() {
    	$('a.nudge').forEach(function(link){
    	    var $link = $(link), left = $link.style('paddingLeft');
    		$link.hover(function(e){
    		    $link.anim({ paddingLeft: /enter|over/.test(e.type) ? 20 : left})
    		})
    	});
    });
  2. Couldn’t agree more on what you said: “The best way to learn to use any javascript framework is to duplicate a given set of code you are familiar with”

    By the way, I think the Mootools code is more semantic than the rest, I mean, with jQuery one has to know that .hover is an event, unlike MooTools which says .addEvent.

    I liked Dojo’s aproch, but the .connect is new to me, but just by reading it I understood that it will connect link to the event and the function.

    Very nice example and keep up the good work.

  3. Jack Franklin

    Hi Rafael / David

    @Rafael F P Viana: You can use jQuery’s bind method which is the same as MooTool’s addEvent essentially.

    Good post David, I actually really like Dojo’s layout so I might investigate. However, it’s clear that MT/jQ are the top two so I think I’m concentrating my skill on those two.

  4. Brad Pritchard

    Nice one. One note/question – if your anchors needed a default padding-left of 15px, then the effect would be muted a bit. Rather than setting padding-left to a static 20, what about changing line 5 to a relative 20?


    dojo.anim(link, { paddingLeft: (left + 20) });

  5. @Brad Pritchard: Much better — I’ll update the post!

  6. Sid Burn

    Do you just limiting to Dojo Base? In Dojo Core there is dojo.behavior that has some additional functionality.
    http://www.dojotoolkit.org/api/dojo/behavior.html

    With dojo.behavior you can write your code like this:

    dojo.require("dojo.behavior");
    dojo.ready(function(){
      dojo.behavior.add({
        'a.nude': {
          'onmouseenter': function(evt) { 
            dojo.anim(evt.target, { paddingLeft:20 }); 
          },
          'onmouseleave': function(evt) {
            dojo.anim(evt.target, { paddingLeft:left });
          }
        }
      });
      dojo.behavior.apply();
    });
    

    The good part on dojo.behavior is that you can call dojo.behavior.apply() as often as you like. It only updates your DOM incremental. Meaning it only adds the events to newly created DOM nodes, or only add things to a node that you added after the last call to dojo.behavior.apply().

    That means you can do some ajax add nodes to your DOM, and call dojo.behavior.apply() after it. And the new nodes get your defined behavior.

  7. @Sid Burn: Thanks for the tip Sid. By the way, I’ve never given a link the “nude” tag before. :)

  8. Sid Burn

    Ah “nude” oder “nudge” for a non-native english speaker it is almost the same. ;)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!