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.

Fade Out the Modal Window<br />

CHAPTER 7 ■ ENHANCING THE USER INTERFACE WITH JQUERY<br />

First, you need to add effects to fade out the modal window. This function will be triggered in several<br />

ways, some of which also trigger events; to h<strong>and</strong>le this, you create a conditional statement that checks<br />

whether an event was triggered, then prevent the default action if that’s the case.<br />

Next, remove the class active from all links, since none of them are in use when the modal window<br />

isn’t visible.<br />

Finally, you select <strong>and</strong> fade out the modal window using .fadeOut(). In the callback function of<br />

.fadeOut(), the modal window will be removed from the DOM entirely.<br />

You add this function <strong>by</strong> inserting the following bold code in the fx object literal:<br />

// Functions to manipulate the modal window<br />

fx = {<br />

// Checks for a modal window <strong>and</strong> returns it, or<br />

// else creates a new one <strong>and</strong> returns that<br />

"initModal" : function() {<br />

// If no elements are matched, the length<br />

// property will be 0<br />

if ( $(".modal-window").length==0 )<br />

{<br />

// Creates a div, adds a class, <strong>and</strong><br />

// appends it to the body tag<br />

return $("")<br />

.addClass("modal-window")<br />

.appendTo("body");<br />

}<br />

else<br />

{<br />

// Returns the modal window if one<br />

// already exists in the DOM<br />

return $(".modal-window");<br />

}<br />

},<br />

// Fades out the window <strong>and</strong> removes it from the DOM<br />

"boxout" : function(event) {<br />

// If an event was triggered <strong>by</strong> the element<br />

// that called this function, prevents the<br />

// default action from firing<br />

if ( event!=undefined )<br />

{<br />

event.preventDefault();<br />

}<br />

// Removes the active class from all links<br />

$("a").removeClass("active");<br />

// Fades out the modal window, then removes<br />

// it from the DOM entirely<br />

$(".modal-window")<br />

255

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

Saved successfully!

Ooh no, something went wrong!