13.02.2013 Views

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Now we’re fully prepared for users to drag modules around. The last thing that mousedownH2() does is to stop the default<br />

action of the mousedown. This prevents the browser from doing what it normally does when the user presses down the<br />

mouse button. In a drag-and-drop situation, it stops text on the page from being selected while the ghost is being dragged.<br />

As the user moves the mouse around, the document mousemove event listener will be constantly calling<br />

mousemoveDocument():<br />

function mousemoveDocument(event)<br />

{<br />

if (typeof event == "undefined")<br />

{<br />

event = window.event;<br />

}<br />

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

if (ghost != null)<br />

{<br />

ghost.style.marginLeft = event.clientX - dragOrigin[0] + "px";<br />

ghost.style.marginTop = event.clientY - dragOrigin[1] + "px";<br />

}<br />

var closest = null;<br />

var closestY = null;<br />

for (var i in dragHotspots)<br />

{<br />

var ghostX = parseInt(ghost.style.left, 10) + parseInt(ghost.style.marginLeft, 10);<br />

var ghostY = parseInt(ghost.style.top, 10) + parseInt(ghost.style.marginTop, 10);<br />

if (ghostX >= dragHotspots[i].offsetX -➥<br />

dragHotspots[i].element.offsetWidth && ghostX distanceY)<br />

{<br />

closest = dragHotspots[i];<br />

closestY = distanceY;<br />

}<br />

}<br />

}<br />

if (closest != null)<br />

{<br />

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

if (ghostMarker == null)<br />

{<br />

ghostMarker = document.createElement("div");<br />

ghostMarker.id = "ghostMarker";<br />

chapter 9 Creating Dynamic Interfaces Using JavaScript<br />

235

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

Saved successfully!

Ooh no, something went wrong!