html拖拽事件
2018-03-18 17:06
204 查看
需要先设置拖拽元素的draggable属性为true。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> * { margin: 0; padding: 0; } li { list-style: none; float: left; width: 50px; height: 50px; line-height: 50px; text-align: center; border-radius: 50%; background-color: yellow; } .a, .b { width: 300px; height: 300px; border: 1px solid red; } </style> </head> <body> <ul class="a"> <li draggable="true">1</li> <li draggable="true">2</li> <li draggable="true">3</li> <li draggable="true">4</li> </ul> <div class="b"> </div> <!--javascript--> <script> var lis = document.querySelectorAll("li"); var b = document.querySelector(".b"); var srcEle; lis.forEach(function (element, index) { //拖动就会触发 // element.ondrag = function () { // console.log("拖拽元素drag"); // } //拖动开始的时候 element.ondragstart = function () { srcEle = index; console.log("拖拽元素dragstart"); } //离开自己的时候 element.ondragleave = function () { console.log("拖拽元素dragleave"); } //手放下的那一刻 element.ondragend = function () { console.log("拖拽元素dragend"); } } ) //拖动元素时,鼠标进入目标元素后会触发一次! b.ondragenter = function () { console.log("目标元素ondragenter"); } //拖动元素时,鼠标进入目标元素后会一直触发 b.ondragover = function (e) { e = e || window.event; e.preventDefault(); console.log("目标元素ondragover"); } b.ondragleave = function () { console.log("目标元素ondragleave"); } //拖动元素在目标元素上面放下的时候触发 b.ondrop = function () { this.appendChild(lis[srcEle]); console.log("目标元素ondrop"); } </script> </body> </html>
相关文章推荐
- html drag拖拽事件
- HTML之鼠标拖拽事件
- HTML5实战与剖析之原生拖拽(二拖拽事件dragstart、drag和dragend)
- SystemUI 拖拽事件分析
- 纯HTML5+JS制作 物理 拖拽 山地车~!
- Create.js拖拽事件处理机制
- javascript简单拖拽实现代码(鼠标事件 mousedown mousemove mouseup)
- Silverlight学习笔记(七)-----Silverlight事件处理之鼠标事件实现简单拖拽
- JavaScript中IE与DOM事件的区别http://www.cnblogs.com/stpangpang/archive/2008/06/03/1212607.html
- Html 内嵌 选择器属性 Dom操作 JavaScript 事件
- html标签input 响应回车事件
- <html5+css3+js>事件触发js代码的不同方式
- 如何让动态创建的页面元素响应用户事件 (HTML/javascript/jQuery/Ajax/DOM)
- 浅析html input 等值改变添加监听事件
- 将事件写在html中就能执行整体事件,写在js里为什么不能执行?
- Flash AS3 显示对象对鼠标单击,双击,拖拽事件的处理
- HTML Button.onclick 事件汇总
- HTML DOM - 事件处理方式
- 完美实现鼠标拖拽事件,解决各种小bug,基于jquery
- 【润乾报表5】HTML事件--鼠标移入高亮显示