02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

356<br />

CHAPTER 10 ■ EXTENDING JQUERY<br />

■ Note The .dequeue() method takes the current animation out of the animation queue, preventing the<br />

animation from jumping to the end when the queue is cleared with .clearQueue(). Allowing the queue to build up<br />

can cause the animated element to look jumpy or to perform the animation many times in rapid succession, which<br />

is definitely an undesirable effect.<br />

Modifying Each Matched Element<br />

Because the .each() method accepts a callback, you can easily modify each matched element in the<br />

<strong>jQuery</strong> object being processed. For the dateZoom plugin, you’ll add hover event h<strong>and</strong>lers to each selected<br />

element.<br />

When a user hovers her mouse over an element to which dateZoom has been applied, the zoom<br />

method will run. This method relies on the fontsize property of the defaults object to enlarge the text<br />

appropriately. When the user stops hovering, the original text size will be passed to zoom, <strong>and</strong> the<br />

element’s text will return to its original size.<br />

To store the original size, use the .css() method <strong>and</strong> place the original font size in a private<br />

variable.<br />

You use the .hover() method to implement this functionality <strong>by</strong> inserting the following bold code<br />

into the dateZoom plugin:<br />

(function($){<br />

// A plugin that enlarges the text of an element when moused<br />

// over, then returns it to its original size on mouse out<br />

$.fn.dateZoom = function(options)<br />

{<br />

// Only overwrites values that were explicitly passed <strong>by</strong><br />

// the user in options<br />

var opts = $.extend($.fn.dateZoom.defaults, options);<br />

};<br />

// Loops through each matched element <strong>and</strong> returns the<br />

// modified <strong>jQuery</strong> object to maintain chainability<br />

return this.each(function(){<br />

// Stores the original font size of the element<br />

var originalsize = $(this).css("font-size");<br />

// Binds functions to the hover event. The first is<br />

// triggered when the user hovers over the element, <strong>and</strong><br />

// the second when the user stops hovering<br />

$(this).hover(function(){<br />

$.fn.dateZoom.zoom(this, opts.fontsize, opts);<br />

},<br />

function(){<br />

$.fn.dateZoom.zoom(this, originalsize, opts);<br />

});<br />

});

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!