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 />

380<br />

Writing the Mousemove Event Listener<br />

One note of caution for those who are bad at math. There is some possibility that your head will explode<br />

trying to comprehend how move() calculates left and top during the drag. So, maybe cover your ears<br />

and close your eyes while we work on move(). Someone will give you a poke when we’re done to let you<br />

know it’s safe to follow along again.<br />

Now even though drag() will have returned prior to JavaScript ever calling move(), which it will do<br />

rapid-fire during a drag, wrapper continues to refer to the Running by way of a closure. Therefore,<br />

we can reposition the by changing wrapper.style.left and wrapper.style.top.<br />

This is where things get tricky. Even though the clientX and clientY members of an event object are<br />

in window coordinates, but CSS values for left and top are in document coordinates, we can use the<br />

former to calculate the latter for the reason that the document does not scroll during the drag, while<br />

move() is running.<br />

With this in mind, we can calculate the CSS value for wrapper.style.left by adding the X<br />

coordinate of the mousemove event in e.clientX to the local variable left and then subtracting the X<br />

coordinate of the mousedown event. Finally, we concatenate "px" to that number:<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 />

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

var clientX = e.clientX;<br />

var clientY = e.clientY;<br />

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

addListener(document, "mousemove", move, true);<br />

addListener(document, "mouseup", drop, true);<br />

burst(e);<br />

thwart(e);<br />

function move(e) {<br />

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

wrapper.style.left = left + e.clientX - clientX + "px";<br />

}<br />

function drop(e) {<br />

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

}<br />

}<br />

In the same way, we can calculate the CSS value for wrapper.style.top by adding the Y coordinate<br />

of the mousemove event in e.clientY to the local variable top and then subtracting the Y coordinate of<br />

the mousedown event. Finally, we concatenate "px" to that number:<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 />

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

var clientX = e.clientX;<br />

var clientY = e.clientY;<br />

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

addListener(document, "mousemove", move, true);<br />

addListener(document, "mouseup", drop, true);

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

Saved successfully!

Ooh no, something went wrong!