Download presentation
Presentation is loading. Please wait.
Published byFay Fleming Modified over 9 years ago
1
INTRODUCTION TO HTML5 Drag and Drop in HTML5
2
Browsers Support Currently, most of the major HTML5 desktop web browsers support the new HTML5 drag-and-drop attributes and events, except for Opera 11.50. HTML5 native drag-and-drop is nonexistent in today’s HTML5- aware mobile web browsers.
3
Identify Draggable Elements Elements that you intend to be clicked and dragged must be identified as draggable, using the draggable attribute. When this is set, HTML5 web browsers will fire off additional browser events that you can use in JavaScript to follow the drag-and-drop process. This attribute causes no visual change to the element; that must be handled by CSS. For example, you can indicate to the user that an element is draggable by changing the mouse cursor displayed over the element:
4
Identify Drop Zone Elements There are two ways to identify elements as a drop zone, or in other words, accept draggable elements. Note that the first method is recommended, as it is supported by the most web browsers.
5
The dragenter, dragover, and dragleave Event Methods The first and primary method involves simply assigning drop- point elements a specific class name, such as dropzone, and then using that class name in JavaScript to register listener functions for your drag-and-drop events.
6
The dropzone Attribute Method The alternative method involves using the new dropzone attribute. None of the major web browsers support this attribute, but once implemented, it will allow you to simplify the dragenter, dragover, and dragleave events.
7
The dropzone Attribute Method The feedback parameter can be copy, move, or link and sets the mouse icon effect. Only one feedback value can be specified, but if unspecified, it defaults to copy. The kind parameter is a single character that states the acceptable data type. Use f for accepting files dragged from the desktop and s for accepting data strings. type is the MIME type of the data accepted. Multiple kind:type parameters can be specified, if required.
8
The dropzone Attribute Method For example, if you were copying image files dragged from the desktop, the dropzone attribute would be If you were moving a data string, use
9
Dragstart Event The dragstart event occurs on the element that has the draggable=’true’ attribute set. When you register this event listener on a draggable element, you will be notified when the user has clicked down and initiated the drag-and-drop process. Your registered dragstart event function can be used to fade out the draggable element object, to use event.dataTransfer.setData() to store supplementary information in the dragged item, and to assign a drag image under the mouse cursor.
10
Drag Event The drag event occurs on the element that has the draggable=’true’ attribute set. When you register this event listener on a draggable element, you will be persistently notified while the user is actively dragging something. This event is not commonly used but can be if you need to know exactly where the draggable element is on the web page, as it is being dragged.
11
Dragenter Event The dragenter event fires for every element the mouse drag enters across the web page. Most web page elements you will not be interested in, so you can isolate your dragenter activity by registering an event listener only on drop zone elements. Your registered dragenter event function can be used to highlight a drop zone element area, which provides a visual cue to the user that the element being dragged can be dropped here.
12
Dragover Event The dragover event is very similar to drag and dragenter, except that this event fires continuously only while the user drags and hovers over a drop zone element on the web page. The purpose of this event is to set the drop effect for the user feedback as the user actually moves the mouse.
13
Dragleave Event The dragleave event is also very similar to dragenter, except it fires for every element that the mouse drag leaves across the web page. Again, most elements will be uninteresting, except for those that you have already registered as dragenter and dragover event listeners. Your registered dragleave event function can be used to remove the dragenter highlight on a drop zone element area.
14
Drop Event The drop event occurs on the drop zone element where the user releases the mouse button. This allows you to identify where exactly the user completed the drag-and-drop process and to launch the appropriate JavaScript code to process the user’s action. Your registered drop event function can be used to verify that a drop action was appropriate, such as if the dragged element is compatible with the dropped element.
15
Dragend Event The dragend event occurs after the user releases the mouse button. This differs from the drop event as dragend fires from the draggable element, not the drop zone element. In other words, dragend occurs regardless of whether the drag-and-drop process completed successfully or not. Your registered dragend event function can be used to undo what the dragstart function did. For example, if dragstart faded out the draggable element object, dragend should restore it.
16
Dragging Objects out of a Web Page Only the dragstart and dragend events fire when the drag- and-drop process begins and finishes. All other events are omitted because the drop point is outside of the web browser. The information you store in event.dataTransfer.setData() will then be transferred to your operating system. It is then up to the desktop or application to interpret that data correctly.
17
Dragging Objects into a Web Page Only the dragenter, dragover, dragleave, and drop events fire when dragging something from your desktop into your web page. All other events are omitted because the drag start point is outside of the web browser. The information being brought into your web page can be read using event.dataTransfer.getData(), which is usually a path to the local file.
18
Specify Objects to Drag
19
Apply CSS
20
Specify Objects to Drag
23
Specify a Drop Zone
24
Apply CSS to the dropzone class
25
Specify a Drop Zone
27
Handle the Drag-and-Drop Events
30
After processing the dragstart event, you need to instruct the browser how to handle the data as it is being dragged across the screen. First, cancel the default event using event.preventDefault() and by returning false; second, specify the appropriate drop effect that will actually be displayed to the user:
31
Handle the Drag-and-Drop Events The event.preventDefault() and return false commands both serve the same purpose but apply to different browsers, yet this code is safe to define on all browsers. Preventing the default action purpose is to instruct the browser that this individual function is providing all the functionality for this particular event and that it should therefore avoid the default event actions built into the browser itself. In the case of the dragover event, the default actions all relate to the built-in drag-and-drop functionality in the browser’s own user interface, such as dragging links into either the URL or tab bars.
32
Handle the Drag-and-Drop Events
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.