ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
#用面向对象编写拖拽 ###用面向过程编写拖拽 ``` <style> #div1{ width: 100px; height: 100px; background: red; position: absolute; } </style> <script> window.onload = function () { var oDiv = document.getElementById('div1'); var disX = 0; var disY = 0; oDiv.onmousedown = function(ev){ var ev = ev || window.event; disX = ev.clientX - oDiv.offsetLeft; disY = ev.clientY - oDiv.offsetTop; document.onmousemove = function(ev){ var ev = ev || window.event; oDiv.style.left = ev.clientX - disX + 'px'; oDiv.style.top = ev.clientY - disY + 'px'; }; document.onmouseup = function(){ document.onmousemove = null; document.onmouseup = null; }; return false; }; }; </script> <div id="div1"></div> ``` ###普通方法变型 ``` var oDiv = null; var disX = 0; var disY = 0; window.onload = function () { oDiv = document.getElementById('div1'); init(); }; function init(){ oDiv.onmousedown = fnDown; } function fnDown(ev){ var ev = ev || window.event; disX = ev.clientX - oDiv.offsetLeft; disY = ev.clientY - oDiv.offsetTop; document.onmousemove = fnMove; document.onmouseup = fnUp; return false; } function fnMove(ev){ var ev = ev || window.event; oDiv.style.left = ev.clientX - disX + 'px'; oDiv.style.top = ev.clientY - disY + 'px'; } function fnUp(){ document.onmousemove = null; document.onmouseup = null; } ``` ###用面向对象编写拖拽 ``` window.onload = function(){ var d1 = new Drag('div1'); d1.init(); }; function Drag(id){ this.oDiv = document.getElementById(id); this.disX = 0; this.disY = 0; } Drag.prototype.init = function(){ var _this = this; this.oDiv.onmousedown = function(ev){ var ev = ev || window.event; _this.fnDown(ev); return false; }; }; Drag.prototype.fnDown = function(ev){ var _this = this; this.disX = ev.clientX - this.oDiv.offsetLeft; this.disY = ev.clientY - this.oDiv.offsetTop; document.onmousemove = function(ev){ var ev = ev || window.event; _this.fnMove(ev); }; document.onmouseup = this.fnUp; }; Drag.prototype.fnMove = function(ev){ this.oDiv.style.left = ev.clientX - this.disX + 'px'; this.oDiv.style.top = ev.clientY - this.disY + 'px'; }; Drag.prototype.fnUp = function(){ document.onmousemove = null; document.onmouseup = null; }; ```