自訂滾動條是練習物件導向與滑鼠事件的絕佳題目。它需要同時處理三件事:拖曳滑桿、響應滾輪、以及讓滑桿位置與內容捲動量保持比例同步。
頁面上有四個 div:
把這四個元素與相關方法封裝進一個建構函式,就是一個可重複使用的滾動條元件。
* { margin:0; padding:0; }
#div1 { width:30px; height:400px; background:red;
position:relative; top:30px; left:30px; }
#div2 { width:30px; height:30px; background:blue; position:absolute; }
#div3 { width:300px; height:400px; border:1px solid blue;
position:absolute; left:100px; top:30px; overflow:hidden; }
#div4 { position:absolute; top:0; left:0; }
window.onload = function () {
var obj = new ScrollBar("div");
};
function ScrollBar(id) {
var _this = this;
this.oDiv1 = document.getElementById(id + "1"); // 軌道
this.oDiv2 = document.getElementById(id + "2"); // 滑桿
this.oDiv3 = document.getElementById(id + "3"); // 視窗
this.oDiv4 = document.getElementById(id + "4"); // 內容
this.disY = 0; // 拖曳時滑鼠與滑桿的距離
this.oBtn = true; // 滾輪方向標記
// 依比例計算滑桿高度:視窗高 / 內容高 × 軌道高
this.oDiv2.style.height =
this.oDiv3.offsetHeight / this.oDiv4.offsetHeight * this.oDiv1.offsetHeight + "px";
// 滑桿拖曳
this.oDiv2.onmousedown = function (ev) { _this.fnDown(ev); };
// 滾輪:分標準與 IE 兩套事件
if (this.oDiv1.addEventListener) {
this.oDiv1.addEventListener("DOMMouseScroll", function (ev) { _this.toRun(ev); }, false);
this.oDiv4.addEventListener("DOMMouseScroll", function (ev) { _this.toRun(ev); }, false);
}
this.oDiv1.onmousewheel = function (ev) { _this.toRun(ev); };
this.oDiv4.onmousewheel = function (ev) { _this.toRun(ev); };
}
這裡有兩個值得學的細節。第一,用 var _this = this 把 this 存起來,因為事件回呼裡的 this 會指向觸發事件的元素,不再是建構函式的 this。第二,滾輪事件當時有兩套 API:標準瀏覽器的 DOMMouseScroll 與 IE 的 onmousewheel,必須都綁(今天則統一用 wheel 事件)。
ScrollBar.prototype.toRun = function (ev) {
var ev = ev || window.event;
this.abc = 0;
// 判斷滾動方向
if (ev.detail) {
this.oBtn = ev.detail > 0; // Firefox
} else {
this.oBtn = ev.wheelDelta < 0; // IE / Chrome
}
this.abc = this.oBtn ? this.oDiv2.offsetTop + 10 : this.oDiv2.offsetTop - 10;
// 邊界檢查
if (this.abc < 0) {
this.abc = 0;
} else if (this.abc > this.oDiv1.offsetHeight - this.oDiv2.offsetHeight) {
this.abc = this.oDiv1.offsetHeight - this.oDiv2.offsetHeight;
}
this.oDiv2.style.top = this.abc + 'px';
// 依比例換算成內容的位移
this.soleY = this.abc / (this.oDiv1.offsetHeight - this.oDiv2.offsetHeight);
this.oDiv4.style.top =
-this.soleY * (this.oDiv4.offsetHeight - this.oDiv3.offsetHeight) + 'px';
if (ev.preventDefault) ev.preventDefault();
return false;
};
核心公式是 soleY = 滑桿位移 / 滑桿可移動距離,得到一個 0 到 1 之間的比例,再乘上內容的總可捲動距離,就是內容該往上移動多少。這確保了滑桿走到哪、內容就跟到哪。
ScrollBar.prototype.fnDown = function (ev) {
var _this = this;
var ev = ev || window.event;
this.disY = ev.clientY - this.oDiv2.offsetTop; // 記下按下位置與滑桿的偏移
if (this.oDiv2.setCapture) this.oDiv2.setCapture(); // IE 專屬的游標捕獲
document.onmousemove = function (ev) { _this.fnMove(ev); };
document.onmouseup = function () { _this.fnUp(); };
};
ScrollBar.prototype.fnMove = function (ev) {
this.goMove(ev || window.event);
};
ScrollBar.prototype.fnUp = function () {
document.onmousemove = null;
document.onmouseup = null;
if (this.oDiv2.releaseCapture) this.oDiv2.releaseCapture();
};
ScrollBar.prototype.goMove = function (ev) {
this.abc = ev.clientY - this.disY;
if (this.abc < 0) {
this.abc = 0;
} else if (this.abc > this.oDiv1.offsetHeight - this.oDiv2.offsetHeight) {
this.abc = this.oDiv1.offsetHeight - this.oDiv2.offsetHeight;
}
this.oDiv2.style.top = this.abc + "px";
this.scrollX = this.abc / (this.oDiv1.offsetHeight - this.oDiv2.offsetHeight);
this.oDiv4.style.top =
-this.scrollX * (this.oDiv4.offsetHeight - this.oDiv3.offsetHeight) + "px";
if (ev.preventDefault) ev.preventDefault();
return false;
};
這裡有兩個關鍵設計:disY 記錄滑鼠與滑桿頂端的初始偏移量,避免按下滑桿中間時滑桿突然跳到滑鼠位置;以及mousemove 綁在 document 上而非滑桿上,滑鼠游標快速移出滑桿時拖曳不會中斷。
<div id="div1"><div id="div2"></div></div> <div id="div3"><div id="div4"> ...大量文字內容,用來製造溢出以測試捲動... </div></div>
這個版本堪稱完整,但仍有幾處值得優化:
abc、soleY、scrollX 語意不明,改成 currentTop、ratio 會好讀很多offsetHeight,會觸發版面重排(reflow)。可在初始化與 resize 時快取這些數值wheel,並用 passive: false 搭配 preventDefault()transition 讓滾輪捲動帶一點緩衝,手感會更自然整體來說,這個範例把物件導向的封裝、事件生命週期管理、比例換算與邊界處理都串了起來,是很紮實的練習題。