本文實例講述了JavaScript使用面向?qū)ο髮崿F(xiàn)的拖拽功能。分享給大家供大家參考,具體如下:
成都創(chuàng)新互聯(lián)成立于2013年,我們提供高端成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、成都網(wǎng)站設(shè)計公司、網(wǎng)站定制、網(wǎng)絡(luò)營銷推廣、小程序開發(fā)、微信公眾號開發(fā)、成都網(wǎng)站推廣服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計、程序開發(fā)來完成項目落地,為柴油發(fā)電機企業(yè)提供源源不斷的流量和訂單咨詢。
面向?qū)ο笥袀€前提:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>面向?qū)ο蟮睦^承-1</title> <style> #div1 {width: 100px; height: 100px; background: red; position: absolute;} </style> <script> window.onload = function() { var oDiv = document.getElementById('div1'); oDiv.onmousedown = function(ev) { var ev = ev || event; var disX = ev.clientX - this.offsetLeft; var disY = ev.clientY - this.offsetTop; document.onmousemove = function(ev) { var ev = ev || event; oDiv.style.left = ev.clientX - disX + 'px'; oDiv.style.top = ev.clientY - disY + 'px'; } document.onmouseup = function() { document.onmousemove = document.onmouseup = null; } } } </script> </head> <body> <div id="div1"></div> </body> </html>
把局部變量改成全局變量
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>面向?qū)ο蟮睦^承-2</title> <style> #div1 {width: 100px; height: 100px; background: red; position: absolute;} </style> <script> var oDiv=null; var disX=0; var disY=0; window.onload = function() { oDiv = document.getElementById('div1'); oDiv.onmousedown = fnDown; } function fnMove(ev) { var ev = ev || event; oDiv.style.left = ev.clientX - disX + 'px'; oDiv.style.top = ev.clientY - disY + 'px'; } function fnUp() { document.onmousemove = document.onmouseup = null; } function fnDown(ev) { var ev = ev || event; disX = ev.clientX - this.offsetLeft; disY = ev.clientY - this.offsetTop; document.onmousemove = fnMove; document.onmouseup =fnUp; } </script> </head> <body> <div id="div1"></div> </body> </html>
引用塊內(nèi)容
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>面向?qū)ο蟮睦^承-2</title> <style> #div1 {width: 100px; height: 100px; background: red; position: absolute;} #div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;} </style> <script> window.onload=function(){ new Drag('div1'); new Drag('div2'); } function Drag(id) { var _this=this; this.disX=0; this.disY=0; this.oDiv = document.getElementById(id); this.oDiv.onmousedown = function(){ _this.fnDown() }; } Drag.prototype.fnDown=function (ev) { var ev = ev || event; var _this=this; this.disX = ev.clientX - this.oDiv.offsetLeft; this.disY = ev.clientY - this.oDiv.offsetTop; document.onmousemove = function(){ _this.fnMove(); }; document.onmouseup =function(){ _this.fnUp(); }; } Drag.prototype.fnMove=function(ev) { var ev = ev || event; 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 } </script> </head> <body> <div id="div1"></div> <div id="div2"></div> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>面向?qū)ο蟮睦^承-2</title> <style> #div1 {width: 100px; height: 100px; background: red; position: absolute;} #div2 {width: 100px; height: 100px; background: red; position: absolute;top:120px;} </style> <script> window.onload=function(){ new Drag('div1'); new Drag('div2'); } function Drag(id) { var _this=this; this.disX=0; this.disY=0; this.oDiv = document.getElementById(id); this.oDiv.onmousedown = function(){ _this.fnDown() }; } Drag.prototype.fnDown=function (ev) { var ev = ev || event; var _this=this; this.disX = ev.clientX - this.oDiv.offsetLeft; this.disY = ev.clientY - this.oDiv.offsetTop; document.onmousemove = function(){ _this.fnMove(); }; document.onmouseup =function(){ _this.fnUp(); }; } Drag.prototype.fnMove=function(ev) { var ev = ev || event; 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 } </script> </head> <body> <div id="div1"></div> <div id="div2"></div> </body> </html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試一下運行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript頁面元素操作技巧總結(jié)》、《JavaScript操作DOM技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
分享名稱:JavaScript使用面向?qū)ο髮崿F(xiàn)的拖拽功能詳解
網(wǎng)站地址:http://muchs.cn/article8/jehsop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、做網(wǎng)站、網(wǎng)站收錄、網(wǎng)頁設(shè)計公司、域名注冊、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)