按住滑鼠左鍵拖曳,畫面出現淡藍色半透明(opacity:0.3、border:2px solid #0099FF)矩形選取框,鬆開滑鼠自動消失,類似桌面作業系統的多選框選體驗。
function Rect() {
this.doc = document.documentElement;
this.startX = ''; this.startY = '';
this.rectBox = null;
rectSelf = this; // 供內部方法存取實例
}
Rect.prototype.down = function(e) {
e = e || window.event;
this.startX = e.clientX ? e.clientX + document.body.scrollLeft : e.pageX;
this.startY = e.clientY ? e.clientY + document.body.scrollTop : e.pageY;
this.showRect(true);
};
Rect.prototype.move = function(e) {
if (!this.rectBox) return;
var curX = e.clientX ? e.clientX + this.doc.scrollLeft : e.pageX;
var curY = e.clientY ? e.clientY + this.doc.scrollTop : e.pageY;
this.rectBox.style.width = Math.abs(curX - this.startX) + 'px';
this.rectBox.style.height = Math.abs(curY - this.startY) + 'px';
if (curX - this.startX < 0) this.rectBox.style.left = curX + 'px';
if (curY - this.startY < 0) this.rectBox.style.top = curY + 'px';
};
Rect.prototype.up = function(e) {
this.rectBox.style.width = '0px';
this.rectBox.style.height = '0px';
this.showRect(false);
};
Rect.prototype.showRect = function(show) {
if (show) {
if (!this.rectBox) {
this.rectBox = document.createElement('div');
this.rectBox.id = 'rectBox';
document.body.appendChild(this.rectBox);
}
this.rectBox.style.display = 'block';
this.rectBox.style.left = this.startX + 'px';
this.rectBox.style.top = this.startY + 'px';
this.addEventListener(this.doc, 'mousemove', this.move);
} else {
if (this.rectBox) this.rectBox.style.display = 'none';
this.removeEventListener(this.doc, 'mousemove', this.move);
}
};
// 跨瀏覽器事件綁定封裝
Rect.prototype.addEventListener = function(o, e, l) {
if (o.addEventListener) o.addEventListener(e, l, false);
else if (o.attachEvent) o.attachEvent('on' + e, function() { l(window.event); });
};
Rect.prototype.removeEventListener = function(o, e, l) {
if (o.removeEventListener) o.removeEventListener(e, l, false);
else if (o.detachEvent) o.detachEvent('on' + e, function() { l(window.event); });
};
// 初始化
window.onload = function() {
var r = new Rect();
r.addEventListener(r.doc, 'mousedown', r.down);
r.addEventListener(r.doc, 'mouseup', r.up);
};
#rectBox { background:#CCFFFF; border:2px solid #0099FF; filter:alpha(opacity=30); opacity:0.3; position:absolute; }
clientX/Y + scrollLeft/Top (IE) 與 pageX/Y (標準)。left/top 設為終點座標,寬高取絕對值,確保矩形正確繪製。mousemove,鬆開時移除,避免效能浪費。document.documentElement.scrollLeft/Top 確保頁面捲動時座標正確。
const box = document.createElement('div');
box.style.cssText = 'position:fixed; border:2px solid #0099FF; background:#CCFFFF; opacity:0.3; pointer-events:none; display:none;';
document.body.appendChild(box);
let startX, startY;
document.addEventListener('pointerdown', e => {
startX = e.clientX; startY = e.clientY;
box.style.left = startX + 'px'; box.style.top = startY + 'px';
box.style.width = box.style.height = '0px';
box.style.display = 'block';
document.addEventListener('pointermove', onMove);
document.addEventListener('pointerup', onUp);
});
function onMove(e) {
box.style.width = Math.abs(e.clientX - startX) + 'px';
box.style.height = Math.abs(e.clientY - startY) + 'px';
if (e.clientX < startX) box.style.left = e.clientX + 'px';
if (e.clientY < startY) box.style.top = e.clientY + 'px';
}
function onUp() {
box.style.display = 'none';
document.removeEventListener('pointermove', onMove);
document.removeEventListener('pointerup', onUp);
}
pointerdown/move/up 統一處理滑鼠/觸控/觸控筆,position:fixed 免去捲動座標換算,pointer-events:none 讓選取框不阻擋底層元素事件。