23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9 ■ LISTENING FOR EVENTS<br />

366<br />

Now let’s iterate over the Element nodes in elements. Remember from Chapter 4 that looping in<br />

reverse is snappier in that we can test and update the loop variable i in a single expression. Note too that<br />

we want to initialize i to elements.length so that we don’t slow things down by querying<br />

elements.length every roundabout of the loop. Finally, initialize a variable named offsets to null since<br />

it will later contain an array of offsets identifying where the sprites are:<br />

function prepSprites() {<br />

var elements = findClass("sprite"), sprites = {};<br />

for (var i = elements.length, offsets = null; i --; ) {<br />

}<br />

function slideSprite() {<br />

}<br />

}<br />

Now for every Element node in elements, we want to name a member in sprites with the value of its<br />

id attribute. So, relative to our XHTML markup, sprites will contain members named "adidas", "asics",<br />

"brooks", and so on. Those will initially contain an empty array, which we will create with array literal<br />

notation, since that is snappier than doing so with new and Array():<br />

function prepSprites() {<br />

var elements = findClass("sprite"), sprites = {};<br />

for (var i = elements.length, offsets = null; i --; ) {<br />

sprites[elements[i].id] = [];<br />

}<br />

function slideSprite() {<br />

}<br />

}<br />

Now we have to work around some Internet Explorer 8 or earlier skullduggery. Querying<br />

currentStyle.backgroundPosition for an element returns undefined even though querying<br />

style.backgroundPosition for the very same element returns the horizontal and vertical offsets of the<br />

background image. I know, that’s preposterous.<br />

Are those offsets simply missing in currentStyle?<br />

No, they’re just in a different drawer. Internet Explorer 8 or earlier separates them into members<br />

named backgroundPositionX and backgroundPositionY. We will have to code one path for Explorer 9,<br />

Firefox, Safari, Chrome, and Opera and another for Internet Explorer 8 or earlier. Let’s do the former<br />

first. Test for the DOM method getComputedStyle(), and then query backgroundPosition, saving that to<br />

sprites[elements[i].id][0]. So for example, sprites.saucony[0] will contain "0px -135px", which is<br />

the off position for the sprite (the position when the mouse is off the sprite).<br />

Now we need to separate the horizontal and vertical offsets. To do so, call String.split() on the off<br />

position that we just saved to elements[i].id][0]. Remember from Chapter 2 that String.split()<br />

returns an array of smaller strings created by separating the larger string relative to its parameter. So if<br />

we divide the off string based on whitespace, we get an array with two elements. So for the Saucony<br />

sprite, the array would be as follows:<br />

["0px", "-135px"]<br />

Save that to the offsets variable that we initialized to null a moment ago. So we now have this:<br />

function prepSprites() {<br />

var elements = findClass("sprite"), sprites = {};<br />

for (var i = elements.length, offsets = null; i --; ) {<br />

sprites[elements[i].id] = [];<br />

if (typeof getComputedStyle === "function") {<br />

sprites[elements[i].id][0] = queryCascade(elements[i], "backgroundPosition");<br />

offsets = sprites[elements[i].id][0].split(/\s+/);

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

Saved successfully!

Ooh no, something went wrong!