18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Drag and Drop<br />

position: absolute;<br />

left: 300px;<br />

}<br />

<br />

<br />

<br />

Try dragging the red square onto the blue square.<br />

<br />

<br />

<br />

<br />

As you can see, the <strong>JavaScript</strong> section of this code is markedly smaller than that of the example in the<br />

previous section. The first two lines of <strong>JavaScript</strong> create the zDraggable and zDropTarget objects.<br />

Then, the drop target is registered to the zDraggable object by using addDropTarget(). Finally, an<br />

event handler for the drop event is added to the drop target. That event handler moves the draggable<br />

element to the upper-left corner of the drop target by making use of the previously mentioned methods.<br />

Remember, the relatedTarget property of the event object is equal to the draggable element when<br />

used in a drop event.<br />

Of course, all this code must be called after the page has been loaded, so the onload event handler is<br />

used for that purpose.<br />

Summary<br />

This chapter introduced the concept of drag and drop in a <strong>Web</strong> browser and explained the difference<br />

between system drag and drop and simulated drag and drop.<br />

You learned about Internet Explorer’s built-in system drag-and-drop functionality, and that it is the only<br />

browser that supports system drag and drop from <strong>Web</strong> pages. The various events and methods provided<br />

by IE to work with system drag and drop were also discussed, as well as strategies for dragging<br />

text and links.<br />

Next you learned about simulated drag and drop, a way to use the DOM to move elements around that<br />

gives the appearance of drag-and-drop functionality. You were shown how to build a simple drag-anddrop<br />

example.<br />

Lastly, you were introduced to the zDragDrop library, a free <strong>JavaScript</strong> library that encapsulates a great<br />

deal of simulated drag-and-drop functionality. Using this library, you learned how to create a drag-anddrop<br />

example making use of the custom objects, methods, and events.<br />

409

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

Saved successfully!

Ooh no, something went wrong!