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 10 ■ SCRIPTING BOM<br />

420<br />

Swapping Sprites by ID or Class<br />

Oftentimes a number of elements will share the same parts of a sprite. In an e-commerce site, for<br />

example, every Add to Cart link would share the same off and over image. If you have several JavaScript<br />

scrollers on a page, the same thing goes for the arrow sprites.<br />

One way to fix this would be to give each element a unique ID, say add_to_cart_01 through<br />

add_to_cart_72. In addition to being error prone and inefficient, that would be fairly ridiculous.<br />

Numbering ID values won’t do. However, swapping sprites by class would be quite elegant.<br />

Identically styled elements typically are of the same class. So, swapping their sprites by class makes a<br />

good deal of sense.<br />

That’s what we’ll do then. It’s pretty simple to modify prepSprites() and swapSprites(). In the var<br />

bit of the for loop, declare a member variable. Then in the beginning of the for block, initialize member to<br />

id or className by way of the || operator. If id contains "", which is falsey, the || returns the value of the<br />

class attribute, which is a string. For our arrows, that would be "left arrow sprite" or "right arrow sprite".<br />

Insofar as an object member may be named with any string, including "", we’ll next name a member in<br />

sprites with one of those two class strings, but only if sprites does not already have a member member.<br />

In that event, we’ll calculate offsets just like in Chapter 9 except that we need to replace<br />

sprites[elements[i].id] with sprites[member] inasmuch as members are not necessarily named by ID<br />

anymore. So, there are four replacements in the DOM version and three in the Internet Explorer version:<br />

var prepSprites = window.getComputedStyle ?<br />

function () {<br />

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

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

member = elements[i].id || elements[i].className;<br />

if (! sprites[member]) {<br />

sprites[member] = [];<br />

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

offsets = sprites[member][0].split(/\s+/);<br />

sprites[member][1] = 1 - parseInt(queryCascade(elements[i], "width")) + "px " +<br />

offsets[1];<br />

}<br />

addListener(elements[i], "mouseover", slideSprite);<br />

addListener(elements[i], "mouseout", slideSprite);<br />

}<br />

function slideSprite(e) {<br />

if (e.type == "mouseover") {<br />

e.target.style.backgroundPosition = sprites[e.target.id][1];<br />

} else {<br />

e.target.style.backgroundPosition = sprites[e.target.id][0];<br />

}<br />

}<br />

} :<br />

function () {<br />

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

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

member = elements[i].id || elements[i].className;<br />

if (! sprites[member]) {<br />

sprites[member] = [];<br />

offsets = [queryCascade(elements[i], "backgroundPositionX"), queryCascade(elements[i],<br />

"backgroundPositionY")];

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

Saved successfully!

Ooh no, something went wrong!