Summary of HTML5 element drag and drop settings

Source: Internet
Author: User

Put the element picture inside the div box

1. First set the element to be drag and drop: Add draggable= "true" to the IMG tag.

2. Set the drag of the element: Call a function drag (EV) in the IMG tag with the ondragstart attribute, which sets the data type and value of the dragged data using the Datatransfer.setdata () method.

function drag (EV) {

Ev.dataTansfer.setData ("text", ev.target.id)//data type is Text, the value is the ID of the draggable element ("Drag1").

}

3. Where to put-ondragover

Use OnDragOver in the placed Div to specify the allowed settings for the setting, because the default cannot put data/elements into other elements, and if you need to allow placement, you must prevent the default handling of the element.

At this time, a function alloedrop (EV) is called with OnDragOver, and the function internally specifies that the Event.preventdefault () method is used to block the default handling of the element.

<div ondragover= "AllowDrop (event)" ></div>

function AllowDrop (EV) {

Ev.prentdefault ();

}

4, the following is placed-ondrop

Set the ondrop= drop (EV) function in the placed Div, which specifies the placement procedure.

<div ondrop= "Drop (event)" ></div>

Function Drop (EV) {

Ev.preventdefault ();//used to avoid browser's default handling of data

    • var data=ev.datatransfer.getdata ("text"), or//Datatransfer.getdata ("text") method to obtain the dragged data. The method returns any data that is set to the same type in the SetData () method.

Ev.target.appendChild (data);//The dragged element is the ID ("DRAG1") of the dragged element, appended to the drop element (the target element).

}

Summary of HTML5 element drag and drop settings

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.