此插件為早期練習作品,模擬原生捲軸行為。作者誠實列出四大缺憾:
(function(window, undefined) {
var config = { auto: true, height: 'auto', width: 'auto' };
var linkFlyScroll = function(dom, options) {
if (typeof dom === 'string') dom = document.getElementById(dom);
if (!dom || !dom.nodeType) return this;
// 1. 建立容器、深度克隆內容(注意:克隆不含事件綁定)
var scrollObj = document.createElement('div');
var cloneObj = dom.cloneNode(true);
scrollObj.className = 'rollDiv';
scrollObj.appendChild(cloneObj);
dom.parentNode.replaceChild(scrollObj, dom);
return new linkFlyScroll.prototype.init(scrollObj, options || {});
};
linkFlyScroll.prototype.init = function(dom, options) {
var currScroll = this; // 固定 this 指向
if (dom.nodeType) {
currScroll.target = dom; // 容器
currScroll.context = dom.firstChild; // 內容 (克隆節點)
currScroll.options = tool.extend(config, options);
// 尺寸套用
if (currScroll.options.width !== 'auto') dom.style.width = tool.convertValue(currScroll.options.width);
if (currScroll.options.height !== 'auto') dom.style.height = tool.convertValue(currScroll.options.height);
// 跳過文字節點找到真正元素
while (currScroll.context.nodeType !== 1) currScroll.context = currScroll.context.nextSibling;
// 2. 建立捲軸 DOM:上箭頭、軌道、下箭頭
currScroll.scrollUl = document.createElement('ul');
currScroll.scrollUl.className = 'lf_Scroll';
['lfs_Top','lfs_Center','lfs_Bottom'].forEach(function(cls) {
var li = document.createElement('li'); li.className = cls; currScroll.scrollUl.appendChild(li);
});
currScroll.context.style.position = 'relative';
dom.appendChild(currScroll.scrollUl);
// 3. 初始化幾何計算
this.change();
// 4. 綁定滾輪事件
tool.addScrollEvent(currScroll.context, function(e) {
var delta = e.wheel > 0 ? -3 : 3; // 向上/向下 3px
currScroll.scrollEvent.call(currScroll, currScroll.endTop + delta);
});
// 5. 滑鼠拖曳捲軸 (核心邏輯省略,見原碼)
tool.addEvent(currScroll.scrollUl, 'mousedown', function(e) { mouseDown.call(currScroll, e); });
tool.addEvent(document.body, 'mousemove', function(e) {
if (currScroll.isDrag) { /* 計算新位置並呼叫 scrollEvent */ }
});
tool.addEvent(document.body, 'mouseup', function() { currScroll.isDrag = false; });
}
};
// 原型掛載
linkFlyScroll.prototype.init.prototype = linkFlyScroll.prototype;
window.linkFlyScroll = linkFlyScroll;
})(window);
var tool = {
extend: function(target, source) { /* 深度合併物件 */ },
convertValue: function(v) { return typeof v === 'number' ? v+'px' : v; },
addEvent: function(el, type, fn) { /* 相容 addEventListener/attachEvent */ },
addScrollEvent: function(el, fn) { /* 相容 mousewheel/DOMMouseScroll/wheel */ },
getMousePos: function(e) { /* 回傳 {x,y} 相對文檔座標 */ }
};
.lf_Scroll, .lf_Scroll li { padding:0; margin:0; list-style:none; font:14px/24px Helvetica, Arial, 'Microsoft Yahei', sans-serif; outline:none; }
.lf_Scroll { cursor:pointer; width:10px; position:absolute; right:0; top:0; opacity:0.5; filter:alpha(opacity=50); }
.lf_ScrollFocus { opacity:1; filter:alpha(opacity=100); }
.lfs_Top, .lfs_Center, .lfs_Bottom { background:url('ScrollBar.gif'); width:10px; height:10px; }
.lfs_Top { background-position:1px 0; }
.lfs_Center { background-position:center 0; height:100px; }
.lfs_Bottom { background-position:-22px 0; }
.rollDiv { height:100%; width:100%; overflow:hidden; position:relative; }
此原始碼展示了「從零打造 UI 元件」的完整思路:克隆隔離、幾何計算、事件分發、狀態管理。雖有不足,但架構清晰,是學習元件化開發的優質教材。建議讀者以此為基礎,嘗試加入 CSS Transition 動畫、滑鼠懸浮顯示/隱藏、觸控裝置支援等現代特性。