Drag-and-drop is a metaphor familiar on your desktop computers&emdash;you've probably used it for sorting files into folders, adding attachments to emails, and opening a file in a particular program. It's therefore useful when writing web applications to support this drag-and-drop metaphor both within your application and as an interaction method with other content on the user's computer. This functionality is provided in HTML5 in the drag-and-drop API.
The drag-and-drop API makes use of a few properties and a lot of events. The following sequence of diagrams gives you an overview of which events fire when before we dive into code in the following section.
Now that you have an overview, let's examine the details.
To make an element draggable is simple, with a couple of browser compatibility caveats that we'll get to later: add a draggable attribute with value true. This first example has a list of locations in the Columbia Internet offices that you'll make draggable:
On each of the elements, the ondragstart attribute has been set. This function is used to set the data that will be passed by the drag-and-drop action. For now, you'll set the text data as the ID of the element:
Within the function there's also a log command so the sequence of events firing is recorded on the page. As the element with ID 'support' is dragged, the messages are added at the bottom of the page.
The crucial event in the drag-and-drop process is dragOver. Any element that is to be a target for dropped elements must capture the dragOver event and cancel the default action on it:
When the element is dropped, the drop event is fired. It's in this event that the actual work needs to be done. This example removes the list item from the source list and adds it to the selected list using the standard appendChild method:
These two events can be bound declaratively to the drop target element:
If you only want drag-and-drop to work in Firefox, Chrome, and Safari, that's all the code you need&emdash;you're done! If you want it to work in IE too, read ahead to the next section.
Although the HTML5 drag-and-drop API is based on what IE5.5 implemented, it's not identical. What that means is that, although it's possible to write cross-browser code for drag-and-drop that works across Firefox, Chrome/Safari, and IE, doing so isn't as straightforward as the code given in the previous section.
The draggable attribute is an innovation of the HTML5 spec, and IE8 doesn't recognize it. By default, nothing in the previous example is draggable in IE.
Links are draggable by default, so by making everything that should be draggable a link, IE can be supported.
The draggable attribute is no longer required because links are draggable by default.
Older versions of IE use srcElement instead of target.
Do the standard IE support bait-and-switch.
The ondragover attribute doesn't work in IE8 and earlier.
There are two possible approaches to solving this problem. The first approach is to handle the dragenter event, which older versions of IE recognize, instead of the dragover event, which they do not.
The second (and better) approach is to attach the event handler in script rather than declaratively in the HTML markup. The advantage of this approach is that, while attaching the handler declaratively will cause the event to fire, canceling that event won't make the element a drop target in IE8 and earlier. But if you use the proprietary attach-Event() method to attach to the dragOver event in script, canceling that will make the element a drop target. Attaching the event this way is straightforward; the code is shown here:
Note that for advanced developers the best practice is to always attach event handlers in script. For now, this snippet can be copied and pasted into your own code.
Chrome is now broken!
Find the closest parent ID.
You can avoid this issue altogether if the elements you want to drag are links or images&emdash;add the ID directly to those draggable elements. An alternative approach for IE support on elements that aren't draggable by default is to add the links dynamically with script only in IE.
This article has presented one of the most interesting HTML5 APIs, focused on enriching the in-browser experience. We've covered creating word processor-style WYSIWYG editing interfaces and allowing natural drag-and-drop interactions.
Here are some other Manning titles you might be interested in:
HTML5 in Action Robert Crowther, Joe Lennon, Ash Blue and Greg Wanish
HTML5 for .NET Developers Jim Jackson II and Ian Gilman
Sass and Compass in Action Wynn Netherland, Nathan Weizenbaum, and Chris Eppstein
|html5css3001.png ||18.32 KB|
|html5css3002.png ||28.39 KB|
|html5css3008.png ||5.25 KB|
|html5css3003.png ||28.85 KB|
|html5css3005.png ||25.33 KB|
|html5css3004.png ||76.6 KB|
|html5css3006.png ||6.06 KB|
|html5css3007.png ||10.91 KB|
|html5css3009.png ||9.32 KB|
|html5css3010.png ||4.64 KB|
|html5css3011.png ||15.57 KB|
|html5css3012.png ||26.68 KB|
|html5css3013.png ||23.4 KB|
|html5css3014.png ||37.48 KB|
|html5css3015.png ||34.57 KB|
|html5css3016.png ||7.48 KB|
|html5css3017.png ||46.22 KB|
|html5css3018.jpg ||9.84 KB|
|html5css3019.jpg ||24.42 KB|
|html5css3020.png ||28.49 KB|