JS基于面向?qū)ο髮?shí)現(xiàn)的拖拽庫(kù)實(shí)例
本文實(shí)例講述了JS基于面向?qū)ο髮?shí)現(xiàn)的拖拽庫(kù)。分享給大家供大家參考。具體如下:
這是一個(gè)面向?qū)ο蟮腏S拖拽庫(kù),可設(shè)置水平鎖定、垂直鎖定、鎖定位置、鎖定范圍等,設(shè)定這些范圍后,只能在設(shè)定的模式下拖動(dòng),我覺(jué)得這是個(gè)挺不錯(cuò)的拖拽實(shí)例。
運(yùn)行效果截圖如下:
在線演示地址如下:
http://demo.jb51.net/js/2015/js-mxdx-draw-plug-codes/
具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>拖拽庫(kù)</title> <style type="text/css"> div,h2,p{margin:0;padding:0;} body{font:14px/1.5 arial;} #box{width:100px;height:100px;background:#fef4eb;padding:5px;margin:50px;border:1px solid #f60;} #box .title{height:25px;background:#f60;} #tool{margin-bottom:10px;} </style> <script type="text/javascript"> function Drag() { //初始化 this.initialize.apply(this, arguments) } Drag.prototype = { //初始化 initialize : function (drag, options) { this.drag = this.$(drag); this._x = this._y = 0; this._moveDrag = this.bind(this, this.moveDrag); this._stopDrag = this.bind(this, this.stopDrag); this.setOptions(options); this.handle = this.$(this.options.handle); this.maxContainer = this.$(this.options.maxContainer); this.maxTop = Math.max(this.maxContainer.clientHeight, this.maxContainer.scrollHeight) - this.drag.offsetHeight; this.maxLeft = Math.max(this.maxContainer.clientWidth, this.maxContainer.scrollWidth) - this.drag.offsetWidth; this.limit = this.options.limit; this.lockX = this.options.lockX; this.lockY = this.options.lockY; this.lock = this.options.lock; this.onStart = this.options.onStart; this.onMove = this.options.onMove; this.onStop = this.options.onStop; this.handle.style.cursor = "move"; this.changeLayout(); this.addHandler(this.handle, "mousedown", this.bind(this, this.startDrag)) }, changeLayout : function () { this.drag.style.top = this.drag.offsetTop + "px"; this.drag.style.left = this.drag.offsetLeft + "px"; this.drag.style.position = "absolute"; this.drag.style.margin = "0" }, startDrag : function (event) { var event = event || window.event; this._x = event.clientX - this.drag.offsetLeft; this._y = event.clientY - this.drag.offsetTop; this.addHandler(document, "mousemove", this._moveDrag); this.addHandler(document, "mouseup", this._stopDrag); event.preventDefault && event.preventDefault(); this.handle.setCapture && this.handle.setCapture(); this.onStart() }, moveDrag : function (event) { var event = event || window.event; var iTop = event.clientY - this._y; var iLeft = event.clientX - this._x; if (this.lock) return; this.limit && (iTop < 0 && (iTop = 0), iLeft < 0 && (iLeft = 0), iTop > this.maxTop && (iTop = this.maxTop), iLeft > this.maxLeft && (iLeft = this.maxLeft)); this.lockY || (this.drag.style.top = iTop + "px"); this.lockX || (this.drag.style.left = iLeft + "px"); event.preventDefault && event.preventDefault(); this.onMove() }, stopDrag : function () { this.removeHandler(document, "mousemove", this._moveDrag); this.removeHandler(document, "mouseup", this._stopDrag); this.handle.releaseCapture && this.handle.releaseCapture(); this.onStop() }, //參數(shù)設(shè)置 setOptions : function (options) { this.options = { handle: this.drag, //事件對(duì)象 limit: true, //鎖定范圍 lock: false, //鎖定位置 lockX: false, //鎖定水平位置 lockY: false, //鎖定垂直位置 maxContainer: document.documentElement || document.body, //指定限制容器 onStart: function () {}, //開(kāi)始時(shí)回調(diào)函數(shù) onMove: function () {}, //拖拽時(shí)回調(diào)函數(shù) onStop: function () {} //停止時(shí)回調(diào)函數(shù) }; for (var p in options) this.options[p] = options[p] }, //獲取id $ : function (id) { return typeof id === "string" ? document.getElementById(id) : id }, //添加綁定事件 addHandler : function (oElement, sEventType, fnHandler) { return oElement.addEventListener ? oElement.addEventListener(sEventType, fnHandler, false) : oElement.attachEvent("on" + sEventType, fnHandler) }, //刪除綁定事件 removeHandler : function (oElement, sEventType, fnHandler) { return oElement.removeEventListener ? oElement.removeEventListener(sEventType, fnHandler, false) : oElement.detachEvent("on" + sEventType, fnHandler) }, //綁定事件到對(duì)象 bind : function (object, fnHandler) { return function () { return fnHandler.apply(object, arguments) } } }; //應(yīng)用 window.onload = function () { var oBox = document.getElementById("box"); var oTitle = oBox.getElementsByTagName("h2")[0]; var oSpan = document.getElementsByTagName("span")[0]; var oDrag = new Drag(oBox, {handle:oTitle, limit:false}); var aInput = document.getElementsByTagName("input"); //鎖定范圍接口 aInput[0].onclick = function () { oDrag.limit = !oDrag.limit; this.value = oDrag.limit ? "取消鎖定范圍" : "鎖定范圍" }; //水平鎖定接口 aInput[1].onclick = function () { oDrag.lockX = !oDrag.lockX; this.value = oDrag.lockX ? "取消水平鎖定" : "水平鎖定" }; //垂直鎖定接口 aInput[2].onclick = function () { oDrag.lockY = !oDrag.lockY; this.value = oDrag.lockY ? "取消垂直鎖定" : "垂直鎖定" }; //鎖定位置接口 aInput[3].onclick = function () { oDrag.lock = !oDrag.lock; this.value = oDrag.lock ? "取消鎖定位置" : "鎖定位置" }; //開(kāi)始拖拽時(shí)方法 oDrag.onStart = function () { oSpan.innerHTML = "開(kāi)始拖拽" }; //開(kāi)始拖拽時(shí)方法 oDrag.onMove = function () { oSpan.innerHTML = "left:" + this.drag.offsetLeft + ", top:" + this.drag.offsetTop }; //開(kāi)始拖拽時(shí)方法 oDrag.onStop = function () { oSpan.innerHTML = "結(jié)束拖拽" }; }; </script> </head> <body> <div id="tool"> <input type="button" value="鎖定范圍" /> <input type="button" value="水平鎖定" /> <input type="button" value="垂直鎖定" /> <input type="button" value="鎖定位置" /> </div> <p>拖放狀態(tài):<span>未開(kāi)始</span></p> <div id="box"> <h2 class="title"></h2> </div> </body> </html>
希望本文所述對(duì)大家的JavaScript程序設(shè)計(jì)有所幫助。
- JavaScript使用面向?qū)ο髮?shí)現(xiàn)的拖拽功能詳解
- JS基于面向?qū)ο髮?shí)現(xiàn)的拖拽功能示例
- jquery方法+js一般方法+js面向?qū)ο蠓椒▽?shí)現(xiàn)拖拽效果
- Sortable.js拖拽排序使用方法解析
- js實(shí)現(xiàn)拖拽效果
- javascript實(shí)現(xiàn)移動(dòng)端上的觸屏拖拽功能
- 使用js實(shí)現(xiàn)的簡(jiǎn)單拖拽效果
- js完美的div拖拽實(shí)例代碼
- javascript支持firefox,ie7頁(yè)面布局拖拽效果代碼
- JS面向?qū)ο缶幊虒?shí)現(xiàn)的拖拽功能案例詳解
相關(guān)文章
Javascript合并表格中具有相同內(nèi)容單元格示例
表格相同內(nèi)容單元格的合并在以前也有過(guò)類(lèi)似的教程,本文為大家講解的是使用Javascript合并,具體示例及效果圖如下,感興趣的朋友可以參考下2013-08-08js實(shí)現(xiàn)的類(lèi)似QQ的等級(jí)的代碼
類(lèi)似QQ等級(jí)2008-09-09JavaScript定時(shí)器實(shí)現(xiàn)無(wú)縫滾動(dòng)圖片
這篇文章主要為大家詳細(xì)介紹了JavaScript定時(shí)器實(shí)現(xiàn)無(wú)縫滾動(dòng)圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05前端CryptoJS加密、后端JAVA解密代碼實(shí)現(xiàn)參考
這篇文章主要介紹了前端CryptoJS加密、后端JAVA解密代碼實(shí)現(xiàn)參考,需要的朋友可以參考下2023-12-12js結(jié)合正則實(shí)現(xiàn)國(guó)內(nèi)手機(jī)號(hào)段校驗(yàn)
這篇文章主要介紹了js結(jié)合正則實(shí)現(xiàn)國(guó)內(nèi)手機(jī)號(hào)段校驗(yàn)的方法以及使用js和jQuery實(shí)現(xiàn)的簡(jiǎn)單校驗(yàn)手機(jī)號(hào)的示例,非常簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-06-06你必須了解的JavaScript中的屬性描述對(duì)象詳解(下)
JavaScript提供了一個(gè)內(nèi)部數(shù)據(jù)結(jié)構(gòu),用來(lái)描述對(duì)象的屬性,控制它的行為,比如該屬性是否可寫(xiě)、可遍歷等等。這個(gè)內(nèi)部數(shù)據(jù)結(jié)構(gòu)稱(chēng)為“屬性描述對(duì)象”。本文主要帶大家了解一下JavaScript中你必須了解的屬性描述對(duì)象,需要的可以參考一下2022-12-12JavaScript監(jiān)聽(tīng)鍵盤(pán)事件代碼實(shí)現(xiàn)
這篇文章主要介紹了JavaScript監(jiān)聽(tīng)鍵盤(pán)事件代碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06