코드 복사 코드는 다음과 같습니다. 拖拽库<br>div, h2,p{margin:0;padding:0;}<br>body{font:14px/1.5 arial;}<br>#box{width:100px;height:100px; background:#fef4eb;padding:5px;margin :50px;border:1px solid #f60;}<br>#box .title{height:25px;ground:#f60;}<br>#tool{margin-bottom:10px;}<br> <br>함수 Drag()<br>{<br> //初始化<br> this.initialize.apply(this, 인수)<br>}<br>Drag.prototype = {<br> //初始化<br> initialize : 함수(드래그, 옵션)<br> {<br> this.drag = this.$(drag);<br> this._x = this ._y = 0;<br> this._moveDrag = this.bind(this, this.moveDrag);<br> this._stopDrag = this.bind(this, this.stopDrag);<br><br> this.setOptions (옵션);<br><br> this.handle = this.$(this.options.handle);<br> this.maxContainer = this.$(this.options.maxContainer);<br><br> this .maxTop = Math.max(this.maxContainer.clientHeight, this.maxContainer.scrollHeight) - this.drag.offsetHeight;<br> this.maxLeft = Math.max(this.maxContainer.clientWidth, this.maxContainer.scrollWidth) - this.drag.offsetWidth;<br><br> this.limit = this.options.limit;<br> this.lockX = this.options.lockX;<br> this.lockY = this.options.lockY;<br> this.lock = this.options.lock;<br><br> this.onStart = this.options.onStart;<br> this.onMove = this.options.onMove;<br> this.onStop = this.options .onStop;<br><br> this.handle.style.cursor = "이동";<br><br> this.changeLayout();<br><br> this.addHandler(this.handle, "mousedown" , this.bind(this, this.startDrag))<br> },<br> changeLayout : function ()<br> {<br> this.drag.style.top = this.drag.offsetTop "px";<br> this.drag.style.left = this.drag.offsetLeft "px";<br> this.drag.style.position = "absolute";<br> this.drag.style.margin = "0"<br> },<br> startDrag : 함수(이벤트)<br> { <br> var 이벤트 = 이벤트 || window.event;<br><br> this._x = event.clientX - this.drag.offsetLeft;<br> this._y = event.clientY - this.drag.offsetTop;<br><br> this.addHandler (document, "mousemove", this._moveDrag);<br> this.addHandler(document, "mouseup", this._stopDrag);<br><br> event.preventDefault && event.preventDefault();<br> this .handle.setCapture && this.handle.setCapture();<br><br> this.onStart()<br> },<br> moveDrag : 함수(이벤트)<br> {<br> var event = event | | window.event;<br><br> var iTop = event.clientY - this._y;<br> var iLeft = event.clientX - this._x;<br><br> if (this.lock) return;<br><br> this.limit && (iTop < 0 && (iTop = 0), iLeft < 0 && (iLeft = 0), iTop > this.maxTop && (iTop = this.maxTop), iLeft > this.maxLeft && (iLeft = this.maxLeft));<br><br> this.lockY || (this.drag.style.top = iTop "px");<br> this.lockX || (this.drag.style.left = iLeft "px");<br><br> event.preventDefault && event.preventDefault();<br><br> this.onMove()<br> },<br> stopDrag : 함수()<br> {<br> this.removeHandler(document, "mousemove", this._moveDrag);<br> this.removeHandler(document, "mouseup", this._stopDrag);<br><br> this.handle.releaseCapture && this.handle.releaseCapture();<br><br> this.onStop()<br> },<br> //参数设置<br> setOptions : 함수(옵션)<br> {<br> this.options =<br> {<br> 핸들: this.drag, //事件对象<br> 한도: true, //锁定范围<br> 잠금: false, //锁定位置<br> lockX: false, //锁정수평位置<br> lockY: false, //锁定垂直位置<br> maxContainer: document.documentElement || document.body, //指定限system容器<br> onStart: function () {}, //开始时回调函数<br> onMove: function () {}, //拖拽时回调函数<br> onStop: function () {} //停止时回调函数<br> };<br> for (옵션의 var p) this.options[p] = options[p]<br> },<br> //ID 가져오기<br> $ : function (id)<br> {<br> return typeof id === "string" ? document.getElementById(id) : id<br> },<br> //바인딩 이벤트 추가<br> addHandler: function (oElement, sEventType, fnHandler)<br> {<br> return oElement.addEventListener ? oElement.addEventListener(sEventType, fnHandler, false) : oElement.attachEvent("on" sEventType , fnHandler)<br> },<br> //바운드 이벤트 제거<br> RemoveHandler: function (oElement, sEventType, fnHandler)<br> {<br> return oElement.removeEventListener ? oElement.removeEventListener(sEventType, fnHandler, false) : oElement.detachEvent("on" sEventType, fnHandler)<br> },<br> //이벤트를 객체에 바인딩<br> 바인딩: function(object, fnHandler)<br> {<br> return function( )<br> {<br> return fnHandler.apply(객체, 인수) <br> }<br> }<br>}; <p> </p> <p> </p> <p>//Application<br>window.onload = function ()<br>{<br> var oBox = document.getElementById("box") <br> var oTitle = oBox.getElementsByTagName("h2")[ 0];<br> var oSpan = document.getElementsByTagName("span")[0];<br> var oDrag = new Drag(oBox, {handle:oTitle,limit:false});<br><br> var aInput = document.getElementsByTagName("input");<br><br> //범위 인터페이스 잠금<br> aInput[0].onclick = function ()<br> {<br> oDrag.limit = !oDrag.limit ;<br> this.value = oDrag.limit ? "범위 잠금 해제" : "범위 잠금"<br> };<br><br> //수평 잠금 인터페이스<br> aInput[1].onclick = 함수( ) <br> {<br> oDrag.lockX = !oDrag.lockX;<br> this.value = oDrag.lockX ? "수평 잠금 취소": "수평 잠금"<br> };<br><br> / / 수직 잠금 인터페이스<br> aInput[2].onclick = function ()<br> {<br> oDrag.lockY = !oDrag.lockY;<br> this.value = oDrag.lockY ? "수직 잠금 취소": " 수직 잠금"<br> };<br><br> //위치 잠금 인터페이스<br> aInput[3].onclick = function ()<br> {<br> oDrag.lock = !oDrag.lock;<br> this.value = oDrag.lock ? "위치 잠금 해제" : "위치 잠금"<br> };<br><br> //드래그 시작 시 방법<br> oDrag.onStart = function ()<br> {<br> oSpan.innerHTML = "드래그 시작" <br> };<br><br> //드래그 시작 시 방법<br> oDrag.onMove = function ()<br> {<br> oSpan .innerHTML = "left :" this.drag.offsetLeft ", top:" this.drag.offsetTop <br> };<br><br> //드래그 시작 시 방법<br> oDrag.onStop = function () <br> {<br> oSpan.innerHTML = "드래그 종료" <br> };<br>};<br> < input type="button" value="수직 잠금" /> 드래그 앤 드롭 상태: 시작되지 않음