Html5 drag and drop

Source: Internet
Author: User

Html5 drag and drop

1) create a source project

1.1) value of the draggable attribute:

True -- this element can be dragged;

False -- the element cannot be dragged;

Auto-the browser can decide whether an element can be dragged;

1.2) events of the dragged element:

Dragstart -- triggered when the element is dragged;

Drag-triggered repeatedly when an element is dragged;

Dragend -- triggered when the drag operation is complete;

2) create a release zone

2.1) release zone event:

Dragenter -- triggered when the dragged element enters the screen space occupied by the release area;

Dragover -- triggered when the dragged element is triggered in the release zone;

Dragleave -- triggered when the dragged element leaves the release zone if it is not put;

Drop -- triggered when the dragged element is put down in the release area;

 

   

Drop Here

<Script> var src = document. getElementById (src); var target = document. getElementById (target); var msg = document. getElementById (msg); var draggedID; target. ondragenter = handleDrag; target. ondragover = handleDrag; function handleDrag (e) {e. preventDefault ();} target. ondrop = function (e) {var newElem = document. getElementById (draggedID ). cloneNode (false); target. innerHTML =; target. appendChild (newElem); e. preventDefault ();} src. ondragstart = function (e) {draggedID = e.tar get. id; e.tar get. classList. add (dragged);} src. ondragend = function (e) {var elems = document. querySelectorAll (. dragged); for (var I = 0; I <elems. length; I ++) {elems [I]. classList. remove (dragged) ;}}</script> 3) use the DataTransfer object

3.1) the object distributed together with the event triggered by the drag-and-drop operation is DragEvent, which is derived from MouseEvent.

Additional attributes defined by the DragEvent object:

DataTransfer -- returns the object used to transmit data to the release zone (DataTransfer );

3.2) properties defined by the DataTransfer object:

Types -- format of the returned data;

GetData (<script> var target = document. getElementById (target); var httpRequest; var fileList; document. getElementById (submit ). onclick = handleButtonPress; target. ondragenter = handleDrag; target. ondragover = handleDrag; function handleDrag (e) {e. preventDefault ();} target. ondrop = function (e) {fileList = e. dataTransfer. files; e. preventDefault ();} function handleButtonPress (e) {e. preventDefault (); var form = document. getElementById (fruitform); var formData = new FormData (form); if (fileList | true) {for (var I = 0; I <fileList. length; I ++) {formData. append (file + I, fileList [I]) ;}} httpRequest = new XMLHttpRequest (); httpRequest. onreadystatechange = handleResponse; httpRequest. open (POST, form. action); httpRequest. send (formData);} function handleResponse () {if (httpRequest. readyState = 4 & httpRequest. status = 200) {var data = JSON. parse (httpRequest. responseText); document. getElementById (results ). innerHTML = You ordered + data. total + items ;}}</script>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.