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

the Brooks , the would follow their mouse. They’d never be able to click the Brooks —or any<br />

other link on the page. Great googly moogly, we can’t have that!<br />

Let’s register drop() on document, too. Pass true for the fourth parameter again to improve<br />

performance in Internet Explorer 9, Firefox, Safari, Chrome, and Opera. Rather than wait for the<br />

mouseup event to descend and ascend the DOM tree, just nip it in the bud:<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 />

function move(e) {<br />

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

}<br />

function drop(e) {<br />

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

}<br />

}<br />

Now call burst() and thwart(), remembering to pass in e, which contains the mousedown event<br />

object. Doing so prevents any mousedown event listeners bound to ancestors of the from running<br />

and a context menu from appearing for Mac visitors:<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 />

}<br />

function drop(e) {<br />

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

}<br />

}<br />

We’re done with the mousedown event listener. Now let’s move on to move(), mousemove event<br />

listener. Sorry for the pun.<br />

379

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

Saved successfully!

Ooh no, something went wrong!