Drag and drop in the HTML5

Source: Internet
Author: User
Tags string format

1. Steps to implement drag and drop

① set the Object's properties to drag-and-drop, which is draggable= "true"

② writes a handler function for the Drag-and-drop Event.

2. Using the DataTransfer object

The DataTransfer object is a property of the event object that is used to pass the string format data from the dragged element to the drop Target. The Datatranfer object is accessed in the event handler for the Drag-and-drop event because it is the property of the event object

Properties of the ①datatransfer

DropEffect property: represents the visual effect of a drag-and-drop operation that must allow the specified value to be none, copy, link, move, within the range of the allowed visual effects specified with the effectallowed Property.

effectallowed property: used to specify the visual effects allowed when an element is dropped, you can specify a value of none, copy, copylink, copymove, link, linkmove, move, All.

Type attribute: a pseudo-array of strings stored in the type of Data.

Methods of ②datatransfer

void ClearData method: Clears the data stored in the DataTransfer object, and clears the entire data if the argument is Omitted.

void SetData method: The data is stored inside the DataTransfer object.

Domstring GetData method: reads data from the DataTransfer Object.

void Setdragimage method: Set the Drag-and-drop icon with the IMG element

The SetData () method stores data in the DataTransfer object at the start of a drag-and-drop, uses the Type property to specify the data mime type, and the GetData () method reads the data from the DataTransfer object at the end of the Drag. The ClearData () method can be used to clear data within a datatransfer Object.

3. Set the visual effect when dragging and dropping

The DropEffect property is combined with the Effectallowed property to set the visual effect when dragging and dropping.

The Effectallowed property indicates that the visual effect allowed when an element is dragged is typically set in the ondragstart event, allowing the set value to be none, copy, copylink, copymove, link, linkmove, move, All

The DropEffect property represents the visual effect of the actual drag-and-drop, typically specified in the OnDragOver event, allowing the set value to be none, copy, link, Move.

The actual visual effect represented by the DropEffect property must be within the scope of the allowed visual effects represented by the Effectallowed property

Rules:

If the Effectallowed property is set to None. Drag and drop elements are not allowed.

If the DropEffect property is set to none, it is not allowed to be dragged into the target Element.

If the Effectallowed property is set to all or not set, the DropEffect property allows to be set to any value and is displayed as a specified visual Effect.

If the Effectallowed property is set to a specific effect and the DropEffect property has a specific visual effect, then the two specific effect values must be exactly equal, otherwise drag-and-drop elements are not allowed to be dragged into the target Element.

4. Custom drag and drop icons

5. Example: Select a graphic

<!DOCTYPE HTML><HTML><HeadLang= "en">    <MetaCharSet= "UTF-8">    <title></title>    <styletype= "text/css">#div1, #div2, #div3, #div4{Border:3px Dashed #ccc;float: left;margin:10px;Min-height:100px;padding:10px;width:220px;        }    </style>    <Scripttype= "text/javascript">        //Turn off default processing        functionAllowDrop (e) {e.preventdefault (); }        //Drag-and-drop Operations        functiondrag (e) {//set the passed object           //e.datatransfer.setdata ("Text", e.target.id);E.datatransfer.setdata ("Text", e.target.id); //set a custom drag icon            varDragIcon=Document.createelement ('img'); DRAGICON.SRC= 'Img/dragimg.png'; varDT=e.datatransfer; Dt.setdragimage (dragicon,-Ten, -Ten); }        functionDrop (ev) {ev.preventdefault (); //gets the object passed over            varData=Ev.dataTransfer.getData ("Text"); //adding a new object to the objectEv.target.appendChild (document.getelementbyid (data)); }    </Script></Head><Body>     <DivID= "div1"Ondrog= "drop (event)"OnDragOver= "allowDrop (event)">         <H2>Select Color</H2>         <imgsrc= "img/bg_01.png"draggable= "true"ondragstart= "drag (event)"ID= "drag1" />         <imgsrc= "img/bg_02.png"draggable= "true"ondragstart= "drag (event)"ID= "drag2" />         <imgsrc= "img/bg_03.png"draggable= "true"ondragstart= "drag (event)"ID= "drag3" />     </Div>     <DivID= "div2"OnDrop= "drop (event)"OnDragOver= "allowDrop (event)">         <H2>Red</H2>     </Div>     <DivID= "div3"OnDrop= "drop (event)"OnDragOver= "allowDrop (event)">         <H2>Green</H2>     </Div>     <DivID= "div4"OnDrop= "drop (event)"OnDragOver= "allowDrop (event)">         <H2>Blue</H2>     </Div></Body></HTML>

Drag and drop in the HTML5

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.