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 8 ■ SCRIPTING CSS<br />

Clear Firebug, but do not refresh Firefox because we want queryCascade() to remain available in<br />

memory. Then save the to a variable named myDIV. Pass myDIV and "left" to queryCascade(), which<br />

will then return "40px". Convert that string to the number 40 by way of parseInt(), and then add 10.<br />

Next, convert the sum of 50 to the string "50", and append "px". Finally, write that string, "50px", to the<br />

style attribute. Click Run repeatedly until left is set to "700px". JavaScript will move our running shoe<br />

menu to the right by 10 pixels each time you click Run.<br />

■ Note If you have forgotten how parseInt() works, flip back to Chapter 2. Datatype conversion is covered<br />

there, too.<br />

function queryCascade(element, property) {<br />

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

return getComputedStyle(element, null)[property];<br />

} else if (element.currentStyle) {<br />

return element.currentStyle[property];<br />

}<br />

}<br />

var myDIV = document.getElementById("running");<br />

myDIV.style.left = parseInt(queryCascade(myDIV, "left")) + 10 + "px";<br />

Verify your work with Figure 8–13.<br />

337

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

Saved successfully!

Ooh no, something went wrong!