Html5 drag operation HTML5 drag-and-drop operations on Webpage elements _ html5 tutorial skills-

Source: Internet
Author: User
Before HTML5, to implement drag-and-drop operations on Webpage elements, you must rely on mousedown, mousemove, mouseup, and other APIs through a large amount of JS Code, currently, html5 greatly simplifies the drag-and-drop programming of web page elements. In addition to supporting drag-and-drop of browser elements, APIS also support the mutual drag of data between browsers and other applications, to implement drag-and-drop operations on Webpage elements, you must rely on APIs such as mousedown, mousemove, and mouseup through a large amount of JS Code. HTML5 introduces APIs that directly support drag-and-drop operations, this greatly simplifies the drag-and-drop programming of web page elements. In addition to supporting drag-and-drop of browser elements, these APIS also support data dragging between browsers and other applications.

This article uses a simple example to demonstrate how to use the drag-and-drop API in HTML5.

Scenario:

As shown in, we need to implement:

Drag and Drop the photo from the "album" area on the left to the "bin" area on the right. During the Drag and Drop Process, the "tip" area should be promptly reminded and the drag and drop operation is ongoing;


Implementation Method:

The HTML code on the above interface is relatively simple, as follows:

The Code is as follows:





HTML5 drag-and-drop operations





Tip: You can drag the photo to the bin.



Album





Waste bin







Note: To implement drag-and-drop operations, add the draggable = "true" attribute to the element to be dragged-and-drop;

Next, add the following JS Code to the onload event. The annotations are more detailed and will not be explained separately.

The Code is as follows:


Script
Function init (){
Var info = document. getElementById ("info ");
// Obtain the drag-and-drop element. In this example, it is the DIV of the album.
Var src = document. getElementById ("album ");
// Start the drag-and-drop operation
Src. ondragstart = function (e ){
// Obtain the ID of the dragged photo
Var dragImgId = e.tar get. id;
// Obtain the dragged Element
Var dragImg = document. getElementById (dragImgId );
// The drag-and-drop operation ends.
DragImg. ondragend = function (e ){
// Restore reminder Information
Info. innerHTML = "Tip: You can drag the photo to the bin ";
};
E. dataTransfer. setData ("text", dragImgId );
};
// During the drag-and-drop process
Src. ondrag = function (e ){
Info. innerHTML = "-- the photo is being dragged --";
}
// Obtain the drag-and-drop target Element
Var target = document. getElementById ("trash ");
// Disable the default processing;
Target. ondragenter = function (e ){
E. preventDefault ();
}
Target. ondragover = function (e ){
E. preventDefault ();
}
// Drag something to the target Element
Target. ondrop = function (e ){
Var draggedID = e. dataTransfer. getData ("text ");
// Obtain the DOM object in the album
Var oldElem = document. getElementById (draggedID );
// Delete the photo node from the album DIV
OldElem. parentNode. removeChild (oldElem );
// Add the dragged photo DOM node to the bin DIV;
Target. appendChild (oldElem );
Info. innerHTML = "Tip: You can drag the photo to the bin ";
E. preventDefault ();
}
}
Script


Effect:

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.