Oh hey, look! It’s another blog post—and this one
is cross-posted on hacks.mozilla.com.
I won’t say this is the start of a renewed blogging habit, but let’s see what happens.
Drag and drop is one of the most fundamental interactions
afforded by graphical user interfaces. In one gesture, it
allows users to pair the selection of an object with the
execution of an action, often including a second object in the
operation. It’s a simple yet powerful UI concept used to
support copying, list reordering, deletion (ala the Trash / Recycle Bin),
and even the creation of link relationships.
Since it’s so fundamental, offering drag and drop in web
applications has been a no-brainer ever since browsers first
offered mouse events in DHTML. But, although
mousedown, mousemove, and
mouseup made it possible, the implementation has been
limited to the bounds of the browser window. Additionally,
since these events refer only to the object being dragged,
there’s a challenge to find the subject of the drop when
the interaction is completed.
Of course, that doesn’t prevent most modern JavaScript
frameworks from abstracting away most of the problems and
throwing in some flourishes while they’re at it. But, wouldn’t
it be nice if browsers offered first-class support for drag and
drop, and maybe even extended it beyond the window sandbox?
As it turns out, this very wish is answered by the HTML 5 specification
section on new drag-and-drop events, and
Firefox 3.5 includes an implementation of those events.
If you want to jump straight to the code, I’ve put together
some simple demos
of the new events.
I’ve even scratched an itch of my own and
built the beginnings of an outline editor,
where every draggable element is also a drop target—of which
there could be dozens to hundreds in a complex document, something
that gave me some minor hair-tearing moments in the past
while trying to make do with plain old mouse events.
And, all the above can be downloaded or cloned from
a GitHub repository
I’ve created especially for this article—which continues after the jump.
Read More »