HTML5 Drag and drop
Drag and drop
Drag and drop is a common feature, that is, grabbing an object and dragging it to another location.
In HTML5, drag and drop is part of the standard, and any element can be dragged and dropped.
Related examples:
<html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>html5 drag & drop 拖拽与拖放测试</title> <style> body{font-size:84%;} .dustbin{width:100px; height:260px; line-height:1.4; background-color:gray; font-size:36px; font-family:"微软雅黑", "Yahei Mono"; text-align:center; text-shadow:-1px -1px #bbb; float:left;} .dragbox{width:500px; padding-left:20px; float:left;} .draglist{padding:10px; margin-bottom:5px; border:2px dashed #ccc; background-color:#eee; cursor:move;} .draglist:hover{border-color:#cad5eb; background-color:#f0f3f9;} .dragremind{padding-top:2em; clear:both;} </style> </head> <body> <div class="dustbin"><br>垃<br>圾<br>箱</div> <div class="dragbox"> <div class="draglist" title="拖拽我" draggable="true">列表1</div> <div class="draglist" title="拖拽我" draggable="true">列表2</div> <div class="draglist" title="拖拽我" draggable="true">列表3</div> <div class="draglist" title="拖拽我" draggable="true">列表4</div> <div class="draglist" title="拖拽我" draggable="true">列表5</div> <div class="draglist" title="拖拽我" draggable="true">列表6</div> </div> <div class="dragremind"></div> <script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script><script> var $ = function(selector) { if (!selector) { return []; } var arrEle = []; if (document.querySelectorAll) { arrEle = document.querySelectorAll(selector); } else { var oAll = document.getElementsByTagName("div"), lAll = oAll.length; if (lAll) { var i = 0; for (i; i<lAll; i+=1) { if (/^\./.test(selector)) { if (oAll[i].className === selector.replace(".", "")) { arrEle.push(oAll[i]); } } else if(/^#/.test(selector)) { if (oAll[i].id === selector.replace("#", "")) { arrEle.push(oAll[i]); } } } } } return arrEle; }; var eleDustbin = $(".dustbin")[0], eleDrags = $(".draglist"), lDrags = eleDrags.length, eleRemind = $(".dragremind")[0], eleDrag = null; for (var i=0; i<lDrags; i+=1) { eleDrags[i].onselectstart = function() { return false; }; eleDrags[i].ondragstart = function(ev) { ev.dataTransfer.effectAllowed = "move"; ev.dataTransfer.setData("text", ev.target.innerHTML); ev.dataTransfer.setDragImage(ev.target, 0, 0); eleDrag = ev.target; return true; }; eleDrags[i].ondragend = function(ev) { ev.dataTransfer.clearData("text"); eleDrag = null; return false }; } eleDustbin.ondragover = function(ev) { ev.preventDefault(); return true; }; eleDustbin.ondragenter = function(ev) { this.style.color = "#ffffff"; return true; }; eleDustbin.ondrop = function(ev) { if (eleDrag) { eleRemind.innerHTML = '<strong>"' + eleDrag.innerHTML + '"</strong>被扔进了垃圾箱'; eleDrag.parentNode.removeChild(eleDrag); } this.style.color = "#000000"; return false; }; </script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-11205167-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </body> </html>
Set elements to be draggable
First, in order to make the element draggable, set the draggable attribute to true:
<img draggable="true">
What to drag - ondragstart and setData()
Then, specify what will happen when the element is dragged.
In the above example, the ondragstart attribute calls a function, drag(event), which specifies the data to be dragged.
dataTransfer.setData() method sets the data type and value of the dragged data:
function drag(ev) { ev.dataTransfer.setData("Text",ev.target.id); }
In this example, the data type is "Text" and the value is the id of the draggable element ( "drag1").
Where to place - ondragover
ondragover event specifies where to place the dragged data.
By default, data/elements cannot be placed into other elements. If we need to allow placement, we must prevent the default handling of the element.
This is done by calling the event.preventDefault() method of the ondragover event:
event.preventDefault()
placed - ondrop
When the dragged data is placed, the drop event will occur.
In the above example, the ondrop attribute calls a function, drop(event):
function drop(ev) { ev.preventDefault(); var data=ev.dataTransfer.getData("Text"); ev.target.appendChild(document.getElementById(data)); }
Code explanation:
Call preventDefault() to prevent the browser from modifying the data The default processing (the default behavior of the drop event is to open as a link)
Get the dragged data through the dataTransfer.getData("Text") method. This method will return any data set to the same type in the setData() method.
The dragged data is the id of the dragged element ("drag1")
Append the dragged element to the placed element (target element)
Summary of relevant knowledge points:
DataTransfer object: The medium used to transfer the drop object, usually Event.dataTransfer.
draggable attribute: The label element must be set with draggable=true, otherwise it will have no effect, for example:
<div title="Drag me" draggable="true">list 1</div>
ondragstart event: an event triggered when the dragged element starts to be dragged. This event acts on the dragged element
ondragenter event: when the dragged element enters the target element The event is triggered when the drag element is moved on the target element. This event acts on the target element.
ondragover event: The event is triggered when the drag element moves on the target element. This event acts on the target element.
ondrop Event: An event triggered when the dragged element is on the target element and the mouse is released. This event acts on the target element.
ondragend Event: An event triggered when the drag is completed. This event acts on the dragged element.
Event.preventDefault() method on the element: prevents the execution of some default event methods. PreventDefault() must be executed in ondragover, otherwise the ondrop event will not be triggered. In addition, if you drag something from other applications or files, especially pictures, the default action is to display the picture or related information, and does not actually execute drop. At this time, you need to use the document's ondragover event to kill it directly.
Event.effectAllowed property: It is the effect of dragging.