Comments: Before HTML5, to implement drag-and-drop operations on Webpage elements, You need to 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 the drag-and-drop operation of browser elements, APIS also support data dragging between browsers and other applications.
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. 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:
<! Doctype html>
<Html>
<Head>
<Title> HTML5 drag-and-drop operation </title>
<Meta charset = "UTF-8"/>
<Style>
. Album
{
Border: 3px dashed # ccc;
Float: left;
Margin: 10px;
Min-height: 100px;
Padding: 10px;
Width: 220px;
}
</Style>
</Head>
<Body ">
<Div id = "info">
<H2> tip: You can drag the photo to the bin. </Div>
<Div id = "album" class = "album">
<H2> album
</Div>
<Div id = "trash" class = "album">
<H2> waste bin </Div>
<Br/>
</Body>
</Html>
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 = "};
E. dataTransfer. setData ("text", dragImgId );
};
// During the drag-and-drop process
Src. ondrag = function (e ){
Info. innerHTML = "}
// 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 = "E. preventDefault ();
}
}
</Script>
Effect: