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 9 ■ LISTENING FOR EVENTS<br />

class. Taking a peek at our markup, that would be the element. So fine, if e does not have a target<br />

member, add one that refers to srcElement for Internet Explorer 8 or earlier:<br />

function drag(e) {<br />

if (!e) e = window.event;<br />

if (!e.target) e.target = e.srcElement;<br />

function move(e) {<br />

if (!e) e = window.event;<br />

}<br />

function drop(e) {<br />

if (!e) e = window.event;<br />

}<br />

}<br />

By the way, if you are wondering whether we will be recoding this as an advance conditional loader,<br />

don’t. Other than redefining e and its members, there’s no workarounds for Internet Explorer 8 or<br />

earlier. So our first cut will be our final one.<br />

Where were we? Right. Now we don’t want to move the running tab, which is to say the <br />

element. Rather, we want to move the wrapping the whole shebang, which is<br />

e.target.parentNode. We’ll save that to a local variable aptly named wrapper. That way, both move() and<br />

drop() can manipulate the later. But one thing we need to do straightaway is make sure wrapper<br />

displays in front of any other content in the document so the user can always see it when dragging. To do<br />

so, set its z-index to the return value of doZ(), a helper function we will write later (remind me if I forget).<br />

We now have this:<br />

function drag(e) {<br />

if (!e) e = window.event;<br />

if (!e.target) e.target = e.srcElement;<br />

var wrapper = e.target.parentNode;<br />

wrapper.style.zIndex = doZ();<br />

function move(e) {<br />

if (!e) e = window.event;<br />

}<br />

function drop(e) {<br />

if (!e) e = window.event;<br />

}<br />

}<br />

We just need to jot down some coordinates for move() to do calculations with later. First, we want to<br />

save CSS values for left and top (stripped of their units of measure) to local variables named, well, left<br />

and top, which we will use later to recalculate the new position of the panel. Then we want to save<br />

clientX and clientY for the mousedown event to local variables named, you guessed it, clientX and<br />

clientY. The clientX and clientY members of the event object provide those coordinates in pixels.<br />

However, those are numbers, not strings. So, no "px" suffix to strip away with parseInt(). Note that<br />

clientX and clientY are not in document coordinates because CSS values for left and top would need<br />

to be when we reposition wrapper (in other words, clientX and clientY are the coordinates on the<br />

current window rather than the position in the document). Still, we need clientX and clientY to<br />

calculate left and top as well as to keep the visitors mouse pinned to the same spot on the Running tab<br />

(you’ll see the actual calculations in the next section). We now have this:<br />

function drag(e) {<br />

if (!e) e = window.event;<br />

if (!e.target) e.target = e.srcElement;<br />

var wrapper = e.target.parentNode;<br />

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

377

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

Saved successfully!

Ooh no, something went wrong!