DIV CSS 佈局教程網

自製 DIV 捲軸:位置與長度即時計算
編輯:JavaScript綜合知識     

用兩個 DIV 互相控制內容捲動——一個裝內容、一個當自訂捲軸,再用 jQuery 把兩者的位置與長度綁在一起。

為什麼要自己做捲軸

除了美觀,自製捲軸還能解決幾個原生捲軸做不到的事:把捲軸樣式統一在各瀏覽器上呈現、讓捲軸位置與其他 UI 元素聯動、或在特定區域做獨立捲動而不影響整頁。

一、初始化:算出滑柄該多長

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");
  }
}

關鍵公式是 滑柄長度 = (容器高度 / 內容總高) × 容器高度。內容越多,滑柄越短——這和瀏覽器原生捲軸的行為一致。內容比容器矮時直接把滑柄藏起來,因為沒有捲動的必要。

減去 19px 是扣除上下箭頭的高度,讓滑柄、箭頭與滑槽加起來剛好等於容器高。

二、頁面載入時的版面配置

$(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"); });
});

拖曳的做法是「先記住滑鼠與滑柄的相對偏移 y1」,之後每次移動就用 clientY - y1 當作滑柄的新位置。記偏移量這一步不能省,否則一按下滑柄就會跳到滑鼠正下方。

兩個 if 是邊界夾制:不能往上越過 scrDefualtTop,也不能往下超出 scrDefualtTop + scrMaxHeight - scrHeight。

把 mousemove 綁在 mousedown 裡、並在 mouseup/mouseout 時 unbind,是為了避免「滑鼠放開了卻還在拖」的常見 bug。

三、雙向同步的兩個函式

// 捲軸動 → 內容跟著動
function ChangeScroll() {
  var scrTop = $("#mainScrollContent").scrollTop();
  scrTop = (scrTop * scrMaxHeight) / document.getElementById("mainScrollContent").scrollHeight
         + scrDefualtTop;
  $("#scrollContent").css("top", scrTop + "px");
}

// 滑柄動 → 內容跟著動
function ChangeScrollContent() {
  var scrTop = $("#scrollContent").css("top");
  var st = parseInt(scrTop.replace("px", ""));
  st = ((st - scrDefualtTop) * document.getElementById("mainScrollContent").scrollHeight) / scrMaxHeight;
  $("#mainScrollContent").scrollTop(st);
}

兩者互為反函式,用同一組比例做正反向換算。必須同時綁定:少了 ChangeScroll,使用者用滾輪捲動時滑柄不會動;少了 ChangeScrollContent,拖滑柄時內容不會動。

四、結構與樣式

<div class="jtc_neir" id="Div1" style="height: 100px;">
  ...內容...
</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 {
  margin: 0 20px; color:#000; 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-face-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; }
.tiao_mid   { width:8px; background:url(../images/scrollbar-3.jpg) -39px center repeat-y; }
.tiao_bottom{ width:8px; height:10px; background:url(../images/scrollbar-3.jpg) -13px bottom no-repeat; }

滑柄用拼圖圖(sprite)實作:一張 scrollbar-3.jpg 裡放了上箭頭、中段、下箭頭三個圖形,用 background-position 的不同偏移各取一段。這樣只要一個 HTTP 請求,也保證三者的視覺風格一致。

scrollbar-* 那一串是 IE 專屬的捲軸配色屬性,用來讓原生捲軸的顏色接近自製版本,避免兩者風格割裂。

今天該怎麼做

這套做法在當年很先進,但現在有更省事的路:::-webkit-scrollbar 與 Firefox 的 scrollbar-width / scrollbar-color 已經能直接美化原生捲軸,不必自刻拖曳邏輯,也自動保有鍵盤與觸控支援。若只是想要浮水印式的滑塊外觀,accent-color 搭配原生 overflow 往往就足夠。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved