23.04.2013 Views

javascript

javascript

javascript

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.

CHAPTER 10 ■ SCRIPTING BOM<br />

410<br />

In addition to binding press() for the mousedown event, we want to add a jump member to both<br />

elements. For the one of the right class, jump will contain -10. On the other hand, jump will be 10<br />

for the of the left class. During an animation, JavaScript will add jump to slide.style.left. That is<br />

to say, pressing down on the right arrow will decrement left for slide by 10 pixels, while pressing down<br />

on the left arrow will increment left for slide by 10 pixels. That happens in just 15 milliseconds, mind<br />

you.<br />

Anyway, this is where re earns its keep. Remember from Chapter 2 that RegExp.test() returns true<br />

if the regular expression matches the string parameter and false if not. If we pass re.test() the value of<br />

the class attribute for both arrows, it will return true for the right arrow and false for the left arrow.<br />

With this in mind, we can initialize jump to the appropriate value by making the call to RegExp.test() the<br />

boolean expression prior to the ? token of the conditional operator:<br />

function prepScrollers() {<br />

var elements = findClass("scroller");<br />

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

(function (scroller) {<br />

var wrapper = findClass("wrapper", scroller)[0];<br />

var slide = findClass("slide", scroller)[0];<br />

var w1 = parseInt(queryCascade(wrapper, "width"));<br />

var w2 = parseInt(queryCascade(slide, "width"));<br />

var timer = null;<br />

slide.style.left = queryCascade(slide, "left");<br />

for (var arrows = findClass("arrow", scroller), i = arrows.length, re = /\bright\b/; i -<br />

-; ) {<br />

addListener(arrows[i], "mousedown", press);<br />

arrows[i].jump = (re.test(arrows[i].className)) ? -10 : 10;<br />

}<br />

})(elements[i]);<br />

}<br />

}<br />

Adding the Press Event Listener<br />

Now on to the event listener press(), which will execute whenever a mousedown event fires on an arrow<br />

. Define an e parameter for the DOM event object. Then if JavaScript defaults e to undefined, assign<br />

window.event to e. Remember, window.event is where Internet Explorer will save details about the<br />

mousedown event. One of those tidbits, window.event.srcElement, will refer to the left or right arrow<br />

. DOM-savvy browsers call that member target. So in Internet Explorer, initialize a new<br />

window.event.target member that refers to window.event.srcElement. That way, e.target refers to an<br />

arrow cross-browser:<br />

function prepScrollers() {<br />

var elements = findClass("scroller");<br />

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

(function (scroller) {<br />

var wrapper = findClass("wrapper", scroller)[0];<br />

var slide = findClass("slide", scroller)[0];<br />

var w1 = parseInt(queryCascade(wrapper, "width"));<br />

var w2 = parseInt(queryCascade(slide, "width"));<br />

var timer = null;<br />

slide.style.left = queryCascade(slide, "left");<br />

for (var arrows = findClass("arrow", scroller), i = arrows.length, re = /\bright\b/; i -<br />

-; ) {

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

Saved successfully!

Ooh no, something went wrong!