色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

JS面向對象編程實現的拖拽功能案例詳解

瀏覽:95日期:2024-05-22 13:06:49

本文實例講述了JS面向對象編程實現的拖拽功能。分享給大家供大家參考,具體如下:

原始的面向過程代碼:

<!DOCTYPE html><html> <head> <meta charset='UTF-8'> <style> #box {width: 100px; height: 100px; background: blue; position: absolute; } </style> <title>拖拽</title> <script> var oBox=null; var disX=0; var disY=0; window.onload=function(){oBox=document.getElementById(’box’);oBox.onmousedown=fnDown; }; //鼠標按下事件 function fnDown(ev){var oEvent = ev||event;disX = oEvent.clientX - oBox.offsetLeft;disY = oEvent.clientY - oBox.offsetTop;document.onmousemove = fnMove;document.onmouseup = fnUp; } //鼠標移動事件 function fnMove(ev){var oEvent=ev||event;oBox.style.left = oEvent.clientX - disX + ’px’;oBox.style.top = oEvent.clientY - disY + ’px’; } //鼠標抬起事件 function fnUp(){document.onmousemove = null;document.onmouseup = null; } </script> </head> <body> <div id='box'></div></body></html>

下面是面向對象的代碼

drag.js

/** * 拖拽 * @param {Object} id div的id */function Drag(id){ this.oBox = document.getElementById(id); this.disX = 0; this.disY = 0; var _this = this; this.oBox.onmousedown = function(){ _this.fnDown(); }}//鼠標按下Drag.prototype.fnDown = function(ev){ var oEvent = ev || event; this.disX = oEvent.clientX - this.oBox.offsetLeft; this.disY = oEvent.clientY - this.oBox.offsetTop; var _this = this; document.onmousemove = function(){ _this.fnMove(); }; document.onmouseup = function(){ _this.fnUp(); };}//鼠標移動Drag.prototype.fnMove = function(ev){ var oEvent= ev || event; this.oBox.style.left = oEvent.clientX - this.disX + ’px’; this.oBox.style.top = oEvent.clientY - this.disY + ’px’;}//鼠標抬起Drag.prototype.fnUp = function(){ document.onmousemove = null; document.onmouseup = null;}

drag.html

<!DOCTYPE html><html> <head> <meta charset='UTF-8'> <style> div {position: absolute; } </style> <title>拖拽</title> <script type='text/javascript' src='http://www.lshqa.cn/js/drag.js' ></script> <script> window.onload = function(){var drag1 = new Drag('box1');var drag1 = new Drag('box2'); }; </script> </head> <body> <div style='background: red;width: 200px;height: 200px;'></div> <div style='background: blue;width: 100px;height: 100px;'></div></body></html>

JS面向對象編程實現的拖拽功能案例詳解JS面向對象編程實現的拖拽功能案例詳解

此拖拽有一個問題,就是沒有控制拖拽出邊界的問題。但我們又不想去修改代碼,那我們怎么做?學過java的應該都知道可以寫一個子類來做一些更加具體的操作,又保留了父類的功能,就是繼承。

html

<script type='text/javascript' src='http://www.lshqa.cn/js/drag.js' ></script><script type='text/javascript' src='http://www.lshqa.cn/js/dragLimit.js' ></script><script> window.onload = function(){ var drag1 = new Drag('box1'); var drag1 = new DragLimit('box2');//藍色是不會超出邊界的 };</script><body> <div style='background: red;width: 200px;height: 200px;'></div> <div style='background: blue;width: 100px;height: 300px;'></div></body>

DragLimit.js:DragLimit繼承自Drag,控制了不能出邊界

/** * 限制邊界的拖拽,繼承自Drag * @param {Object} id */function DragLimit(id){ Drag.call(this, id);}//繼承方法for(var p in Drag.prototype){ DragLimit.prototype[p] = Drag.prototype[p];}/** * 覆寫父類的鼠標移動方法,控制不能移出邊界 */DragLimit.prototype.fnMove = function(ev){ var oEvent= ev || event; var left = oEvent.clientX - this.disX; var top = oEvent.clientY - this.disY; //控制邊界 if(left < 0){ left = 0; } else if(left > document.documentElement.clientWidth-this.oBox.offsetWidth){ left = document.documentElement.clientWidth-this.oBox.offsetWidth; } if(top <= 0){ top = 0; } else if(top > document.documentElement.clientHeight-this.oBox.offsetHeight){ top = document.documentElement.clientHeight-this.oBox.offsetHeight; } this.oBox.style.left = left + ’px’; this.oBox.style.top = top + ’px’;}

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。

更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《javascript面向對象入門教程》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》

希望本文所述對大家JavaScript程序設計有所幫助。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 中国欧美一级毛片免费 | 日本爽快片100色毛片 | 久久精品亚洲乱码伦伦中文 | 欧美日韩一级片在线观看 | 2021国内自拍 | 亚洲国产精品乱码在线观看97 | 欧洲亚洲一区二区三区 | 久久91亚洲精品中文字幕奶水 | 久久精品亚洲一级毛片 | 久久99精品九九九久久婷婷 | 亚洲一级毛片欧美一级说乱 | avtt亚洲一区中文字幕 | 黄色aaa毛片| 欧美成人短视频 | 一级做a爰片久久毛片唾 | 日韩精品一区二区三区不卡 | 免费看欧美一级特黄a毛片 免费看片aⅴ免费大片 | 国产精品免费视频一区 | 成年人免费大片 | 手机看片1024精品国产 | 亚洲 欧美 国产 中文 | 国产欧美另类久久久精品免费 | 成人做爰全过程免费看网站 | 欧美一区二区高清 | 在线观看免费亚洲 | 成人欧美一区二区三区视频 | 成人久久精品一区二区三区 | 欧美亚洲国产视频 | 美国一级毛片完整高清 | 中国性孕妇孕交在线 | 亚洲精品www久久久久久久软件 | 成人国产欧美精品一区二区 | 欧美+日本+国产+在线观看 | 一级毛片免费播放 | 操爽视频 | 免费在线观看一区 | 国产精品亚洲视频 | 亚洲综合伊人色一区 | 成年人黄国产 | 欧美一级免费观看 | 91伊人国产|