HTML5 Drag the canvas/drag and drop

Source: Internet
Author: User

<! DOCTYPE html>

<script type= "Text/javascript" >

function AllowDrop (EV) {

Ev.preventdeafault ();

}

function drag (EV) {

Ev.dataTranster.setData ("Text", ev.target.id);

}

function Drop (EV) {

var data=ev.datatransfer.getdata ("Text");

Ev.target.appendChild (document.getElementById (data));

Ev.preventdefault ();

}

</script>

<body>

<div id = "DIV1" ondrop= "Drop (event)" Ondragover= "AllowDrop (event)" ></div>

</body>


Enables an element to be dragged

Very easy. You just need to change the drag property of an element to draggable, such as the following:

 
   
  
  1. draggable="true"/>
How to drag-ondragstart () and SetData () methods

We then specify the action that will be run when an element is dragged.

In the previous demo, the Ondragstart property called a method, drag (event). This specifies that the data is dragged.

The Datatransfer.setdata () method sets the data type and the data being dragged:

 
   
  
  1. function Drag (ev) {
  2. ev. DataTransfer . SetData ("Text",ev. ) Target . ID );
  3. }

In this example, the data type is "Text". The value is the ID of the dragged element.

Where to place (drop)-OnDragOver

The OnDragOver event specifies where the dragged element can be placed.

By default, the data/element cannot be drop to another element. In order to agree to the drop. You need to block the default processing first.

We can call the Event.preventdefault () method. For example, the following:

 
   
  
  1. Event . Preventdefault ()
Run placement (drop)

When the draggable data is drop. The drop event is triggered.

In the example above. The OnDrop property can call a method, Drop (event):

  
 
  1. function Drop (ev)
  2. {
  3. var Data = EV . DataTransfer . GetData ("Text");
  4. EV . Target . appendchild (document. getElementById (data));
  5. EV . Preventdefault ();
  6. }

The above code:

    • Use Datatransfer.getdata ("Text") to get the data being dragged.

      This method will return whatever data is set in the SetData () method.

    • The data being dragged is the ID of the element ("Drag1") that can be dragged
    • Add a draggable element to the placed element
    • Call the Preventdefault () method to prevent the default browser data processing (for example, to open a link)

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

HTML5 Drag the canvas/drag and drop

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.