06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

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.

[ 83 ]<br />

Chapter 3<br />

All we do here is check to see if the height of the current element is higher than<br />

the current highest (again, 100px by default). If it is higher, then the value of<br />

stickyHeight is assigned to highest. All we're doing, then, is finding the highest<br />

element <strong>and</strong> setting highest to that element's height.<br />

There is one point of interest here. The highest variable is local to the init()<br />

function, <strong>and</strong> is declared outside of the anonymous function declaration. Doesn't<br />

it seem like this variable ought to be out of scope for the function inside our<br />

each() loop?<br />

Yet, as the previous example shows, it is still accessible inside of the function's body.<br />

This works because in <strong>JavaScript</strong>, functions inside other functions retain access to<br />

the variables of their parent functions. In other words, if function a() (the outer<br />

function) encloses the definition of function b() (the inner function), then function<br />

b() will have access to all of function a()'s local variables.<br />

By the time the each() function returns, highest should be set to the pixel height of<br />

the highest sticky node that we will display.<br />

We're ready to move onto the next part of the init() function:<br />

StickyRotate.init = function() {<br />

var stickies = $(".sticky");<br />

};<br />

// If we don't have enough, stop immediately.<br />

if (stickies.size() 0) {<br />

return;<br />

}<br />

var highest = 100;<br />

stickies.each(function () {<br />

var stickyHeight = $(this).height();<br />

if(stickyHeight > highest) {<br />

highest = stickyHeight;<br />

}<br />

});<br />

stickies.hide().css('height', highest + 'px');<br />

StickyRotate.counter = 0;<br />

StickyRotate.stickies = stickies;<br />

stickies.eq(0).fadeIn('slow');<br />

setInterval(StickyRotate.periodicRefresh, 7000);<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

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

Saved successfully!

Ooh no, something went wrong!