13.02.2013 Views

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

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.

234<br />

function getPosition(theElement)<br />

{<br />

var positionX = 0;<br />

var positionY = 0;<br />

while (theElement != null)<br />

{<br />

positionX += theElement.offsetLeft;<br />

positionY += theElement.offsetTop;<br />

theElement = theElement.offsetParent;<br />

}<br />

return [positionX, positionY];<br />

};<br />

Getting the position of an element on a page is overly complicated, but there’s nothing to be done about it. An element can<br />

discern its position only in relation to its “offset parent” (which varies according to the way it is positioned, floated, and so<br />

on). To get its absolute location on the page, we have to recursively work our way up the offset parent tree (using<br />

element.offsetParent), getting the positions of each of the offset parents along the way and adding them together to<br />

return a total.<br />

Once each of the modules inside a module area has been added to dragHotspots, there’s a special case where we add the<br />

module area itself to dragHotspots. This is so we can recognize when the dragged module is below all other elements in the<br />

module area; in which case, we move it right to the end.<br />

The ghost<br />

After the map is created, we need to create the visual element that will indicate to users that they are dragging something.<br />

It’s actually a copy of the module that the user is dragging, and we make it look ghostly by changing its transparency, so I<br />

call it the ghost.<br />

We get the current position of the dragged module using getPosition(), and then we create the new ghost element. It’s an<br />

absolutely positioned empty div wrapped around a direct copy of the dragged module. The empty div acts as a substitute for<br />

the constraints of the module area div, giving the module a width to fill; otherwise, it would expand to a width of 100%. Because<br />

it’s positioned absolutely and inserted at the end of the body element, it’s free to move wherever the user’s mouse cursor moves.<br />

The copy of the dragged module is created using the cloneNode() method. cloneNode() takes one argument that specifies<br />

whether you want the contents of the node to be copied as well, or just the node itself. By calling dragTarget.cloneNode(true),<br />

we are saying we want a copy of dragTarget and its contents. We add that copy to the empty shell and then<br />

position it over the original module, so it looks like the user is dragging the ghost out of its body. By applying a bit of CSS<br />

opacity, we can give the ghost its ghostly appearance:<br />

#ghost .module<br />

{<br />

opacity: 0.65;<br />

filter: alpha(opacity=50);<br />

}<br />

You can see the result in Figure 9-13.<br />

To track where the user is moving the mouse while the button is<br />

pressed, we need to add a mousemove listener to the entire document.<br />

We add a mouseup listener to tell us when the user has<br />

released the mouse button.<br />

Figure 9-13. The ghost copy of a module next to its original

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

Saved successfully!

Ooh no, something went wrong!