本文實作一個可高度自訂的 div 拖曳程式,能透過按鈕切換拖曳方式與位置限制(鎖定範圍、水平、垂直、位置)。
<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();
},
setOptions : function (options) {
this.options = {
handle: this.drag, //事件對象
limit: true, //鎖定范圍
lock: false, //鎖定位置
lockX: false, //鎖定水平
lockY: false, //鎖定垂直
maxContainer: document.documentElement || document.body,
onStart: function () {},
onMove: function () {},
onStop: function () {}
};
for (var p in options) this.options[p] = options[p];
},
$ : 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);
},
bind : function (object, fnHandler) {
return function () { return fnHandler.apply(object, arguments); }
}
};
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 ? "取消鎖定位置" : "鎖定位置" };
oDrag.onStart = function () { oSpan.innerHTML = "開始拖拽"; };
oDrag.onMove = function () { oSpan.innerHTML = "left:" + this.drag.offsetLeft + ", top:" + this.drag.offsetTop; };
oDrag.onStop = function () { oSpan.innerHTML = "結束拖拽"; };
};
</script>
這個 Drag 類別把拖曳邏輯封裝得很完整:透過 options 可設定拖曳把手、是否鎖定範圍/水平/垂直/位置,並提供 onStart、onMove、onStop 三個回呼,方便在拖曳的不同階段插入自訂行為。加上事件綁定時對 IE 與標準瀏覽器的相容處理,是相當值得參考的元件化寫法。