瀏覽器原生的滾動條樣式能改的地方有限(舊 IE 的 scrollbar-face-color 等屬性早已失效),因此要做出與設計稿一致的外觀,常見做法是「隱藏原生滾動條,另外做一根假的」。
這份實作的關鍵是先量出兩側的比例關係:滑塊高度 =(容器高度 ÷ 內容總高度)× 容器高度,滑塊位置則由內容的 scrollTop 按比例換算而來。雙向都要綁定,滑塊拖動會改內容位置,內容捲動也要帶動滑塊。
<script type="text/javascript">
var scrMinHeight = 1; // 滾動條最小高度
var scrMaxHeight = 0; // 滾動條最大高度
var scrDefualtTop = 80; // 滾動條默認位置
var scrHeight = 0;
// 初始化滾動條
function InitScroll() {
scrMaxHeight = $("#mainScrollContent").height(); // 容器高度
scrHeight = document.getElementById("mainScrollContent").scrollHeight; // 內容總高度
scrHeight = parseInt((scrMaxHeight / scrHeight) * scrMaxHeight); // 換算滑塊高度
if (scrHeight <= scrMinHeight) { scrHeight = scrMinHeight; }
if (scrHeight >= scrMaxHeight) { $("#scrollContent").hide(); } // 不需要滾動就隱藏
else {
$("#scrollContent").show();
$("#scrollContent .tiao_mid").css("height", (scrHeight - 19) + "px");
}
}
$(document).ready(function () {
$(".bod").height(($(document).height() - 80) + "px");
$("#mainScrollContent").height(($(document).height() - 125) + "px");
scrMaxHeight = ($(document).height() - 125); // 滾動條最大高度
$("#scrollBody").height(($(document).height() - 125) + "px");
$("#scrollBodyBack").height(($(document).height() - 125) + "px");
InitScroll();
// 內容捲動 → 帶動滑塊
$("#mainScrollContent").scroll(function () {
ChangeScroll();
});
// 拖曳滑塊
var y1 = 0;
$("#scrollContent").mousedown(function (event) {
var scrContentTop = $("#scrollContent").css("top");
y1 = event.clientY - parseInt(scrContentTop.replace("px", ""));
$("#scrollContent").mousemove(function (event) {
if ((event.clientY - y1) < scrDefualtTop) {
$("#scrollContent").css("top", scrDefualtTop + "px");
}
else if ((event.clientY - y1) > (scrDefualtTop + scrMaxHeight - scrHeight)) {
$("#scrollContent").css("top", (scrDefualtTop + scrMaxHeight - scrHeight) + "px");
}
else {
$("#scrollContent").css("top", (event.clientY - y1) + "px");
}
ChangeScrollContent();
});
}).mouseup(function () {
$("#scrollContent").unbind("mousemove");
}).mouseout(function () {
$("#scrollContent").unbind("mousemove");
});
});
// 滑塊移動 → 改變內容位置
function ChangeScrollContent() {
var scrTop = $("#scrollContent").css("top");
var st = parseInt(scrTop.replace("px", ""));
st = ((st - scrDefualtTop) * document.getElementById("mainScrollContent").scrollHeight) / scrMaxHeight;
$("#mainScrollContent").scrollTop(st);
}
// 內容捲動 → 改變滑塊位置
function ChangeScroll() {
var scrTop = $("#mainScrollContent").scrollTop();
scrTop = (scrTop * scrMaxHeight) / document.getElementById("mainScrollContent").scrollHeight + scrDefualtTop;
$("#scrollContent").css("top", scrTop + "px");
}
</script>
結構分四塊:.jtc_neir 是內容區(靠 CSS 隱藏原生滾動條),另外三個 .jtc_tiao 分別是滑道、背景與滑塊,滑塊又由上、中、下三段拼成,中間那段 .tiao_mid 用 repeat-y 的底圖撐開高度。
<div class="jtc_neir" id="Div1" style="height: 100px;">
營業總收入:11.66億元(同比增長-1.75%)
<br />
淨利潤:0.19億元(同比增長23.72%)
<br />
每股收益:0.04元
<br />
淨資產收益率:1.58%
<br />
毛利率:12.22%(同比增長39.89%)
<br />
總資產:30.46億元(同比增長-7.14%)
<br />
...(內容重複多段以製造滾動)
</div>
<div class="jtc_tiao" style="background-color: rgb(196,206,208); z-index: 2;" id="Div2">
</div>
<div class="jtc_tiao" style="background-color: rgb(222,222,222); z-index: 1; width: 16px;"
id="Div3">
</div>
<div class="jtc_tiao" id="Div4">
<div class="tiao_up">
</div>
<div class="tiao_mid">
</div>
<div class="tiao_bottom">
</div>
</div>
值得注意的是 .jtc_neir 上那一整排 scrollbar-* 屬性——這些是 IE5.5 時代的私有屬性,用來把原生滾動條上色。它們在其他瀏覽器完全無效,也正是這個元件需要自己畫一根滾動條的原因。
.jtc_neir{margin-left:20px; margin-right:20px; color:#000000; font-size:12px;
background:none; line-height:32px; overflow-y:scroll;overflow-x:hidden;
scrollbar-3dlight-color:rgb(222,222,222);
scrollbar-arrow-color:rgb(222,222,222);
scrollbar-base-color:rgb(222,222,222);
scrollbar-darkshadow-color:rgb(222,222,222);
scrollbar-face-color:rgb(222,222,222);
scrollbar-highlight-color:rgb(222,222,222);
scrollbar-shadow-color:rgb(222,222,222);}
.jtc_tiao{width:8px; position:absolute; top:80px; right:20px; z-index:10;}
.tiao_up{width:8px; height:10px; background:url(../images/scrollbar-3.jpg) left top no-repeat;
line-height:0px; overflow:hidden;}
.tiao_mid{width:8px; background:url(../images/scrollbar-3.jpg) -39px center repeat-y;
line-height:0px; overflow:hidden;}
.tiao_bottom{width:8px; height:10px; background:url(../images/scrollbar-3.jpg) -13px bottom no-repeat;
font-size:0; line-height:0px; overflow:hidden;}
程式中的 80、125、19 都是硬編碼的魔術數字,代表「頂部工具列高度」、「上下留白」、「滑塊上下端圖片高度」。一旦版面調整,這些數字全都要手動改,非常脆弱。更好的做法是從 DOM 實際量測每個區塊的高度。
拖曳時把 mousemove 綁在滑塊上,滑鼠移出範圍就強制停止拖曳(mouseout 裡 unbind),體驗上會有「拖到一半突然失控」的感覺。標準做法是綁在 document 上,直到 mouseup 才解除。
而最重要的建議是:現在已經不太需要自己刻滾動條了。
::-webkit-scrollbar(Chrome、Safari、Edge)與 scrollbar-width / scrollbar-color(Firefox 121+、Chrome 121+)已經能直接為原生滾動條上色與改寬度,一行 CSS 解決外觀問題,還保留原生滾動的慣性、鍵盤與觸控行為。自己實作滾動條最大的代價,是必須放棄原生滾動的所有好處:觸控板慣性、鍵盤 Page Up / Down、捲動時的效能最佳化、以及無障礙工具對它的辨識。除非有明確的設計需求,否則優先用 CSS 標準屬性會是更划算的選擇。