<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style type="text/css"> #info { width: 100px; height: 100px; background-color: red; border-radius: 50%; position: absolute; } </style> </head> <body> <div id="info"></div> </body> <script> class fn{ constructor(dom){ this.dom=dom } drag(){ this.dom.onmousedown = function(env){ console.log(this) var env = env || window.event; var disX = env.offsetX; var disY = env.offsetY; var that = this; document.onmousemove = function(env){ console.log(this) var env = env || window.event; var L = env.clientX - disX; var T = env.clientY - disY; that.style.left = L + 'px'; that.style.top = T + 'px'; } document.onmouseup = function (){ console.log(this) document.onmousemove = null; } } } } let a=new fn(document.getElementById('info')) a.drag() </script> </html>
這是拖拽哦css