要讓使用者能用滑鼠拖曳調整區塊大小,做法是:在元素四周與四個角放置八個小控制點,每個點對應不同的游標與縮放方向,拖曳時依滑鼠位移量即時更新元素的寬高與位置。
<div id='ss' style="height:100px; width:100px; border:1px solid #000;
position:absolute; left:200px; top:200px;">
<div id="rRightDown"></div>
<div id="rLeftDown"></div>
<div id="rRightUp"></div>
<div id="rLeftUp"></div>
<div id="rRight"></div>
<div id="rLeft"></div>
<div id="rUp"></div>
<div id="rDown"></div>
</div>
#rRightDown, #rLeftDown, #rLeftUp, #rRightUp,
#rRight, #rLeft, #rUp, #rDown {
position: absolute;
background: #C00;
width: 6px;
height: 6px;
z-index: 5;
font-size: 0;
}
#rLeftDown, #rRightUp { cursor: ne-resize; }
#rRightDown, #rLeftUp { cursor: nw-resize; }
#rRight, #rLeft { cursor: e-resize; }
#rUp, #rDown { cursor: n-resize; }
#rRightDown { bottom: -3px; right: -3px; }
#rLeftDown { bottom: -3px; left: -3px; }
#rRightUp { top: -3px; right: -3px; }
#rLeftUp { top: -3px; left: -3px; }
#rRight { right: -3px; top: 50%; }
#rLeft { left: -3px; top: 50%; }
#rUp { top: -3px; left: 50%; }
#rDown { bottom: -3px; left: 50%; }
負的 -3px 讓控制點有一半落在元素邊界外,視覺上更貼齊邊框;z-index 確保控制點不被內容遮住。
var $ = function (id) { return document.getElementById(id); };
var Css = function (e, o) {
for (var i in o) e.style[i] = o[i];
};
var CurrentStyle = function (element) {
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
};
function addListener(element, e, fn) {
element.addEventListener ? element.addEventListener(e, fn, false)
: element.attachEvent("on" + e, fn);
}
function removeListener(element, e, fn) {
element.removeEventListener ? element.removeEventListener(e, fn, false)
: element.detachEvent("on" + e, fn);
}
// 把函式綁定到指定 this,並預先帶入參數
var BindAsEventListener = function (object, fun) {
var args = Array.prototype.slice.call(arguments).slice(2);
return function (event) {
return fun.apply(object, [event || window.event].concat(args));
};
};
// 簡易的 class 工廠
var Class = function (properties) {
var _class = function () {
return (arguments[0] !== null && this.initialize
&& typeof (this.initialize) == 'function')
? this.initialize.apply(this, arguments) : this;
};
_class.prototype = properties;
return _class;
};
var Resize = new Class({
initialize: function (obj) {
this.obj = obj;
this.resizeelm = null;
this.fun = null;
this.original = []; // 記錄起始狀態
this.width = null;
this.height = null;
this.fR = BindAsEventListener(this, this.resize);
this.fS = BindAsEventListener(this, this.stop);
},
set: function (elm, direction) {
if (!elm) return;
this.resizeelm = elm;
addListener(this.resizeelm, 'mousedown',
BindAsEventListener(this, this.start, this[direction]));
return this; // 回傳 this 以支援鏈式呼叫
},
start: function (e, fun) {
this.fun = fun;
var cs = CurrentStyle(this.obj);
this.original = [
parseInt(cs.width), parseInt(cs.height),
parseInt(cs.left), parseInt(cs.top)
];
this.width = (this.original[2] || 0) + this.original[0];
this.height = (this.original[3] || 0) + this.original[1];
addListener(document, "mousemove", this.fR);
addListener(document, "mouseup", this.fS);
},
resize: function (e) {
this.fun(e);
// 拖曳時若游標移出視窗,IE 用 onlosecapture、其他用 onblur 收尾
if (Sys.IE) {
this.resizeelm.onlosecapture = function () { this.fS(); };
} else {
this.resizeelm.onblur = function () { this.fS(); };
}
},
stop: function () {
removeListener(document, "mousemove", this.fR);
removeListener(document, "mouseup", this.fS);
// 取消拖曳過程中可能產生的文字選取
window.getSelection ? window.getSelection().removeAllRanges()
: document.selection.empty();
},
up: function (e) {
this.height > e.clientY
? Css(this.obj, { top: e.clientY + "px", height: this.height - e.clientY + "px" })
: this.turnDown(e);
},
down: function (e) {
e.clientY > this.original[3]
? Css(this.obj, { top: this.original[3] + 'px', height: e.clientY - this.original[3] + 'px' })
: this.turnUp(e);
},
left: function (e) {
e.clientX < this.width
? Css(this.obj, { left: e.clientX + 'px', width: this.width - e.clientX + "px" })
: this.turnRight(e);
},
right: function (e) {
e.clientX > this.original[2]
? Css(this.obj, { left: this.original[2] + 'px', width: e.clientX - this.original[2] + "px" })
: this.turnLeft(e);
},
leftUp: function (e) { this.up(e); this.left(e); },
leftDown: function (e) { this.left(e); this.down(e); },
rightUp: function (e) { this.up(e); this.right(e); },
rightDown: function (e) { this.right(e); this.down(e); },
// 以下四個 turn* 處理「拖過頭」的翻轉情況
turnDown: function (e) { Css(this.obj, { top: this.height + 'px', height: e.clientY - this.height + 'px' }); },
turnUp: function (e) { Css(this.obj, { top: e.clientY + 'px', height: this.original[3] - e.clientY + 'px' }); },
turnRight: function (e) { Css(this.obj, { left: this.width + 'px', width: e.clientX - this.width + 'px' }); },
turnLeft: function (e) { Css(this.obj, { left: e.clientX + 'px', width: this.original[2] - e.clientX + 'px' }); }
});
window.onload = function () {
new Resize($('ss'))
.set($('rUp'), 'up')
.set($('rDown'), 'down')
.set($('rLeft'), 'left')
.set($('rRight'), 'right')
.set($('rLeftUp'), 'leftUp')
.set($('rLeftDown'), 'leftDown')
.set($('rRightDown'), 'rightDown')
.set($('rRightUp'), 'rightUp');
};
up/down 只動 top 與 height,left/right 只動 left 與 width。leftUp 就是 up 加 left,重用而非重寫,這是物件導向重構的常見手法。turn* 方法就是處理這個情況,也是這個實作最容易出 bug 的地方。stop() 中的 selection 清除是必要的。Math.max(minWidth, ...) 限制。這個範例的年代產物痕跡明顯(attachEvent、手写 Bind、currentStyle),但它把縮放互動的核心邏輯講得很透徹:狀態記錄、方向分派、邊界翻轉、事件生命週期。理解之後,用 Pointer Events API 重寫會乾淨許多,也能同時支援觸控與手寫筆。