Drag and drop
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Drag and drop

on

  • 1,254 views

 

Statistics

Views

Total Views
1,254
Views on SlideShare
1,254
Embed Views
0

Actions

Likes
0
Downloads
8
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Drag and drop Presentation Transcript

  • 1. HTML5 DRAG AND DROP
  • 2. Drag and Drop API •  HTML5 comes with a Drag and Drop API that brings native Drag and Drop support to the browser •  This API can also drag elements from browser to external applications and vice-versa
  • 3. At minimum, drag and drop takes ... 1.  Something to drag 2.  A dragstart event 3.  A dragover event 4.  A drop event
  • 4. 1. Something to drag •  Anything can be dragged! •  Need to set the draggable attribute to true <div id="dragMe" draggable="true"> This is draggable </a>
  • 5. Tip: Let the user know they can drag it •  In CSS: [draggable=true] {! cursor: move;! }!
  • 6. 2. Handle the dragstart event $('[draggable]').each(function () {! this.addEventListener('dragstart',! dragStartEventHandler, false);! });! •  In this event you must signal what the thing being dragged can do function dragStartHandler(e) {! e.dataTransfer.effectAllowed = 'move';! }!
  • 7. 3. Handle the dragover event •  Dropping is prevented by default, so you have to tell the target to stop stopping function dragOverHandler(e) {! e.preventDefault();! }!
  • 8. 4. Handle the drop event $('.dropzone').each(function () {! this.addEventListener('dragover', ! dragOverHandler, false);! this.addEventListener('drop', ! dropHandler, false);! });! •  The default drop events already usually countermand whatever we're trying to do, so cancel them function dropHandler(e) {! e.preventDefault();! }!
  • 9. You're transferring an item, from one place to another, so how do you get the data associated with it? The dataTransfer object!
  • 10. Load it up when beginning the drag dragStartEventHandler = function(e) {! e.dataTransfer.effectAllowed = 'move';! e.dataTransfer.setData('id',! e.target.getAttribute("data-id"));! e.dataTransfer.setData('title', ! $('.title', this).text());! }!
  • 11. Pull that same data out of it when dropping dropEventHandler = function (e) {! var id = e.dataTransfer.getData('id');! var t = e.dataTransfer.getData('title');! $('#x').text(title);! var url = '/api/GetDetails/' + id;! $('#x').load(url);! }!
  • 12. Hands on drag and drop
  • 13. More events On the dragged thing • drag • dragstart • dragend On the target • dragenter • dragover • dragleave • drop
  • 14. You can drag stuff to the desktop •  Only dragstart and dragend •  Whatever you put in event.dataTransfer.setData() will be transferred to your OS •  The OS then has the responsibility to interpret that data.
  • 15. You can drag stuff from the desktop •  Only dragenter, dragover, drop, and dragleave fire. •  You read it in JavaScript with event.dataTransfer.getData() •  This is usually a path to the local file
  • 16. function handleDrop(e) {! e.stopPropagation();! e.preventDefault();! var files = e.dataTransfer.files;! for (var i = 0; i < files.length i++) {! // do stuff with files[i] here! }! }!
  • 17. Conclusion •  When boiled down, it is easy to drag and drop •  Make things draggable with 'draggable="true"' •  Wire up the dragstart, dragover, and drop events •  Write and read data in the event.dataTransfer object with setData and getData