<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>拖拽</title> <style> .box{ width: 100px; height: 100px; background: red; position:absolute; } </style> </head> <body> <div id="box" class="box"></div> <script type="text/javascript"> window.onload = function(){ var boxDom = document.getElementById("box"); boxDom.onmousedown = function(e){ var $this = this;//duixiagn var x = e.clientX;//zu var y = e.clientY;// var left = $this.offsetLeft;//toop var top = $this.offsetTop; document.onmousemove = function(e){ var xx = e.clientX;//sdfs var yy = e.clientY; var nl= xx-x + left; var nt = yy-y+top; $this.style.left = nl+"px"; $this.style.top = nt+"px"; }; document.onmouseup= function(){ document.onmousemove = null; document.onmouseup = null; }; } }; </script> </body> </html>
js实现拖拽功能
最新推荐文章于 2023-12-06 16:30:49 发布