HTML5 Drag and drop

Source: Internet
Author: User

1. Two div to drag and drop the picture back and forth, directly on the code:

Style CSS

<style>        #div1, #div2 {            width:400px;            height:300px;            border:1px solid green;        }        #div2 {            border:1px solid red;        } </style>

Dom

<DivID= "Div1"OnDragOver= "OnDragOver (event)"OnDrop= "OnDrop (event)"></Div><BR><DivID= "Div2"OnDragOver= "OnDragOver (event)"OnDrop= "Onbackdrop (event)">    <imgID= "IMG1"src=".. /images/gif.gif "draggable= "true"ondragstart= "Ondragstart (event)"/></Div>

Js

<script>    function  ondragstart (e) {        e.datatransfer.setdata ("text", e.target.id )    }    function  ondragover (e) {        e.preventdefault ();    }     function OnDrop (e) {        e.preventdefault ();        E.target.appendchild (document.getElementById (E.datatransfer.getdata ("text")));     function Onbackdrop (e) {        e.preventdefault ();        E.target.appendchild (document.getElementById (E.datatransfer.getdata ("text")));     </script>

2. Note the following:

A. The function name cannot be the same as the event name, otherwise the error will be dropped when dragging:

B. OnDragOver event name in the DOM with the entry event can not be changed to E, or drag will also error,

Onbackdrop events can be changed into OnDrop,

The above tests are only tested on the Google 41 release.

HTML5 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.