無依賴實現類設計工具的八控制點縮放功能。本文拆解完整架構:控制點佈局、鼠標事件捕獲、座標計算、瀏覽器兼容封裝,附完整可運行代碼。
目標 DIV 四角 + 四邊中點共 8 個控制點,拖拽任意控制點實時縮放,支持:
<div id="ss" class="resizable-box">
<div class="handle handle-rd"></div> <!-- 右下 -->
<div class="handle handle-ld"></div> <!-- 左下 -->
<div class="handle handle-ru"></div> <!-- 右上 -->
<div class="handle handle-lu"></div> <!-- 左上 -->
<div class="handle handle-r"></div> <!-- 右 -->
<div class="handle handle-l"></div> <!-- 左 -->
<div class="handle handle-u"></div> <!-- 上 -->
<div class="handle handle-d"></div> <!-- 下 -->
</div>
.resizable-box {
position: absolute;
left: 200px; top: 200px;
width: 100px; height: 100px;
border: 1px solid #000;
background: #f9f9f9;
}
.handle {
position: absolute;
width: 6px; height: 6px;
background: #c00;
z-index: 5;
font-size: 0; /* 消除 IE6 底部間隙 */
}
/* 定位與光標 */
.handle-rd { bottom: -3px; right: -3px; cursor: nw-resize; }
.handle-ld { bottom: -3px; left: -3px; cursor: ne-resize; }
.handle-ru { top: -3px; right: -3px; cursor: ne-resize; }
.handle-lu { top: -3px; left: -3px; cursor: nw-resize; }
.handle-r { right: -3px; top: 50%; margin-top: -3px; cursor: e-resize; }
.handle-l { left: -3px; top: 50%; margin-top: -3px; cursor: e-resize; }
.handle-u { top: -3px; left: 50%; margin-left: -3px; cursor: n-resize; }
.handle-d { bottom: -3px; left: 50%; margin-left: -3px; cursor: n-resize; }
var Resize = (function() {
// 瀏覽器嗅探 (僅用於事件捕獲差異)
var Sys = (function(ua) {
return {
IE: !!ua.match(/msie ([\d.]+)/),
IE6: !!ua.match(/msie 6\.0/i)
};
})(navigator.userAgent.toLowerCase());
// 工具函數
var $ = function(id) { return document.getElementById(id); };
var Css = function(el, styles) { for (var p in styles) el.style[p] = styles[p]; };
var CurrentStyle = function(el) {
return el.currentStyle || document.defaultView.getComputedStyle(el, null);
};
var addEvent = function(el, type, fn) {
el.addEventListener ? el.addEventListener(type, fn, false) : el.attachEvent('on' + type, fn);
};
var removeEvent = function(el, type, fn) {
el.removeEventListener ? el.removeEventListener(type, fn, false) : el.detachEvent('on' + type, fn);
};
// 綁定上下文
var bind = function(ctx, fn) {
var args = Array.prototype.slice.call(arguments, 2);
return function() { return fn.apply(ctx, args.concat(Array.prototype.slice.call(arguments))); };
};
var bindAsEvent = function(ctx, fn) {
var args = Array.prototype.slice.call(arguments, 2);
return function(e) { return fn.apply(ctx, [e || window.event].concat(args)); };
};
// Resize 類
function ResizeClass(obj) {
this.obj = obj;
this.handle = null; // 當前拖拽的控制點
this.direction = null; // 方向標識
this.origin = []; // [width, height, left, top] 初始值
this.fixedRight = 0; // right = left + width
this.fixedBottom = 0; // bottom = top + height
this.onMove = bindAsEvent(this, this.resize);
this.onStop = bind(this, this.stop);
}
ResizeClass.prototype = {
// 綁定控制點
set: function(handleEl, direction) {
if (!handleEl) return this;
this.handle = handleEl;
addEvent(handleEl, 'mousedown', bindAsEvent(this, this.start, direction));
return this;
},
// 按下開始
start: function(e, direction) {
this.direction = direction;
var style = CurrentStyle(this.obj);
this.origin = [
parseInt(style.width),
parseInt(style.height),
parseInt(style.left),
parseInt(style.top)
];
this.fixedRight = this.origin[2] + this.origin[0];
this.fixedBottom = this.origin[3] + this.origin[1];
addEvent(document, 'mousemove', this.onMove);
addEvent(document, 'mouseup', this.onStop);
// 防止選中文本/圖片
e.preventDefault ? e.preventDefault() : (e.returnValue = false);
// IE 設置捕獲
if (Sys.IE) this.handle.setCapture();
},
// 移動縮放
resize: function(e) {
var clientX = e.clientX, clientY = e.clientY;
var o = this.origin, dir = this.direction;
var minW = 50, minH = 50; // 最小尺寸
switch (dir) {
case 'right':
var w = clientX - o[2];
if (w >= minW) Css(this.obj, {width: w + 'px'});
break;
case 'left':
var w = this.fixedRight - clientX;
if (w >= minW) Css(this.obj, {left: clientX + 'px', width: w + 'px'});
break;
case 'down':
var h = clientY - o[3];
if (h >= minH) Css(this.obj, {height: h + 'px'});
break;
case 'up':
var h = this.fixedBottom - clientY;
if (h >= minH) Css(this.obj, {top: clientY + 'px', height: h + 'px'});
break;
case 'rightDown':
this.resize.call(this, {clientX: clientX, clientY: clientY, direction: 'right'});
this.resize.call(this, {clientX: clientX, clientY: clientY, direction: 'down'});
break;
case 'leftDown':
this.resize.call(this, {clientX: clientX, clientY: clientY, direction: 'left'});
this.resize.call(this, {clientX: clientX, clientY: clientY, direction: 'down'});
break;
case 'rightUp':
this.resize.call(this, {clientX: clientX, clientY: clientY, direction: 'right'});
this.resize.call(this, {clientX: clientX, clientY: clientY, direction: 'up'});
break;
case 'leftUp':
this.resize.call(this, {clientX: clientX, clientY: clientY, direction: 'left'});
this.resize.call(this, {clientX: clientX, clientY: clientY, direction: 'up'});
break;
}
// 防止選中
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
},
// 釋放
stop: function() {
removeEvent(document, 'mousemove', this.onMove);
removeEvent(document, 'mouseup', this.onStop);
if (Sys.IE) this.handle.releaseCapture();
}
};
return ResizeClass;
})();
// 初始化
var resizer = new Resize($('ss'));
resizer.set($('rRightDown'), 'rightDown')
.set($('rLeftDown'), 'leftDown')
.set($('rRightUp'), 'rightUp')
.set($('rLeftUp'), 'leftUp')
.set($('rRight'), 'right')
.set($('rLeft'), 'left')
.set($('rUp'), 'up')
.set($('rDown'), 'down');
| 模塊 | 核心邏輯 |
|---|---|
| 座標基準 | 記錄初始 left/top/width/height,計算 fixedRight/fixedBottom 作為右/下邊界基準 |
| 方向策略 | 8 方向分派對應 resize 方法,角向調用兩個單向方法組合 |
| 事件捕獲 | mousedown 在 handle,mousemove/mouseup 在 document,防止脫離控制點 |
| IE 兼容 | setCapture/releaseCapture 解決鼠標移出窗口丟失事件 |
| 防選中 | preventDefault + getSelection/selection.empty |
Pointer Events 統一鼠標/觸摸/筆事件ResizeObserver 監聽尺寸變化觸發回調resize: both + overflow: auto 原生實現簡單場景<resizable-box>) 實現框架無關理解底層座標計算與事件流,才能在無庫環境下靈活構建交互組件。