The drag-and-drop effect in HTML5 does not require javascript

Source: Internet
Author: User

Comments: Do not use javascript to implement drag-and-drop effects, which has always been what netizens want to see. Second, html5 today provides the following details for you. For more information, see

In Web development, Javascript is needed to implement drag-and-drop effects. Today, let's use Html5 to implement it:
First look at the core html code:

The Code is as follows:
<Div>
<P> drag the yellow small square into the red box </p>
</Div>
<Div id = "item" draggable = "true">
</Div>
<Div id = "drop">
</Div>

The draggable attribute is newly added to html5. It has three values: true, false, auto. true, and false. auto depends on whether the browser supports the draggable attribute. For more information, see the official documentation.
Add a style:

The Code is as follows:
<Style type = "text/css">
# Drop
{
Width: 300px;
Height: 200px;
Background-color: # ff0000;
Padding: 5px;
Border: 2px solid #000000;
}
# Item
{
Width: 100px;
Height: 100px;
Background-color: # ffff00;
Padding: 5px;
Margin: 20px;
Border: 1px dashed #000000;
}
* [Draggable = true] {
-Moz-user-select: none;
-Khtml-user-drag: element;
Cursor: move;
}
*:-Khtml-drag {
Background-color: rgba (238,238,238, 0.5 );
}
</Style>

Then let's look at javascript.:

The Code is as follows:
Function listenEvent (eventTarget, eventType, eventHandler ){
If (eventTarget. addEventListener ){
EventTarget. addEventListener (eventType, eventHandler, false );
} Else if (eventTarget. attachEvent ){
EventType = "on" + eventType;
EventTarget. attachEvent (eventType, eventHandler );
} Else {
EventTarget ["on" + eventType] = eventHandler;
}
}
// Cancel event
Function cancelEvent (event ){
If (event. preventDefault ){
Event. preventDefault ();
} Else {
Event. returnValue = false;
}
}
// Cancel propagation
Function cancelPropagation (event ){
If (event. stopPropagation ){
Event. stopPropagation ();
} Else {
Event. cancelBubble = true;
}
}
Window. onload = function (){
Var target = document. getElementById ("drop ");
ListenEvent (target, "dragenter", cancelEvent );
ListenEvent (target, "dragover", dragOver );
ListenEvent (target, "drop", function (evt ){
CancelPropagation (evt );
Evt = evt | window. event;
Evt. dataTransfer. dropEffect = 'copy ';
Var id = evt. dataTransfer. getData ("Text ");
Target. appendChild (document. getElementById (id ));
});
Var item = document. getElementById ("item ");
Item. setAttribute ("draggable", "true ");
ListenEvent (item, "dragstart", function (evt ){
Evt = evt | window. event;
Evt. dataTransfer. effectAllowed = 'copy ';
Evt. dataTransfer. setData ("Text", item. id );
});
};
Function dragOver (evt ){
If (evt. preventDefault) evt. preventDefault ();
Evt = evt | window. event;
Evt. dataTransfer. dropEffect = 'copy ';
Return false;
}

The above code shows how to use the drag-and-drop Events provided by HTML5. Let's look at the following:
Dragstart
Drag event starts.
Drag
During the drag operation.
Dragenter
Drag is over the target; used to determine if target will accept drop.
Dragover
Drag is over target; used to determine feedback to user.
Drop
Drop occurs.
Dragleave
Drag leaves target.
Dragend
Drag operation ends.
Defines related events to implement desired functions. The above Js is not hard to understand.
You can try it by yourself. Currently, Opera supports the best, and IE does not work well.
Hope to help your Web development.


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.