实现简单的拖动效果

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #div1, #div2
        {float:left; width:100px; height:35px; margin:10px;padding:10px;border:1px solid #aaaaaa;}
    </style>
    <script type="application/javascript">
        function drag(ev){
            ev.dataTransfer.setData("Text",ev.target.id);
        }
        function allowDrag(en){
            en.preventDefault();
        }
        function drop (en) {
            en.preventDefault();
            var data=en.dataTransfer.getData('Text');
            en.target.appendChild(document.getElementById(data))
        }
    </script>
</head>
<body>
    <div id="div1" ondrop="drop(event)" ondragover="allowDrag(event)">
        <img src="123.jpg" width="80" height="80" draggable="true" ondragstart="drag(event)" id="drag2">
    </div>
    <div id="div2" ondrop="drop(event)" ondragover="allowDrag(event)"></div>
</body>
</html>


你可能感兴趣的:(实现简单的拖动效果)