时间:2021-07-01 10:21:17 帮助过:4人阅读
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .box{ width:200px; height:200px; background-color: red; position: absolute; left:0; top:0; } </style> </head> <body> <p class='box'></p> <script> var box = document.querySelector('.box'); box.onmousedown = function(e){ var event = e || window.event; var target = event.target || event.srcElement; var disX = event.clientX - target.offsetLeft; var disY = event.clientY - target.offsetTop; document.onmousemove = function(event){ // 注意:这里要有自己的事件对象 target.style.left = event.clientX - disX + 'px'; target.style.top = event.clientY - disY + 'px'; document.onmouseup = function(){ document.onmousedown = null; document.onmousemove = null; } } } </script> </body> </html>
相关推荐:
js控制文件拖拽并获取拖拽内容实现代码
js简单的表拖拽_javascript技巧
原生JS实现拖拽图片效果_javascript技巧
以上就是原生js如何实现拖拽事件(代码)的详细内容,更多请关注Gxl网其它相关文章!