萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML5 <input type="range"> 滑桿完整指南:屬性、樣式客製化、調色盤實戰與無障礙

為什麼選 <input type="range">?

原生滑桿提供:

  • 零依賴、跨瀏覽器一致行為(Chrome/Firefox/Safari/Edge 現代版本)
  • 內建鍵盤操作(←/→、Home/End、PageUp/Down)
  • 原生無障礙支援(角色 slider、aria-valuemin/max/now 自動映射)
  • 觸控友善(手指拖拽、手勢縮放不干擾)

核心屬性完整對照

屬性類型預設值說明
min數值0最小值
max數值100最大值
step數值/any1步進間距,any 允許任意浮點數
value數值min + (max-min)/2當前值,必須在 min/max 範圍內且符合 step
disabled布林false禁用,不可互動、不提交、灰色顯示
name字串—表單提交鍵名
listID 參考—關聯 <datalist> 提供刻度標記
orient關鍵字horizontalhorizontal | vertical(Firefox/Edge 支援,Chrome 需 CSS writing-mode)

基礎範例:雙向綁定數值顯示

<label for="volume">音量:<output for="volume" id="volOut">50</output></label>
<input type="range" id="volume" name="volume" min="0" max="100" value="50" step="1" oninput="volOut.value = this.value">

<style>
input[type="range"] { width: 100%; max-width: 300px; }
output { font-variant-numeric: tabular-nums; font-weight: 600; color: #2c3e50; }
</style>

<output> 語義化顯示計算結果,for 指向關聯輸入元素 ID。

進階實戰:RGB 調色盤(三聯動滑桿)

<div class="color-mixer">
  <div class="channel">
    <label>R: <output for="r">255</output></label>
    <input type="range" id="r" min="0" max="255" value="255" step="1">
  </div>
  <div class="channel">
    <label>G: <output for="g">128</output></label>
    <input type="range" id="g" min="0" max="255" value="128" step="1">
  </div>
  <div class="channel">
    <label>B: <output for="b">0</output></label>
    <input type="range" id="b" min="0" max="255" value="0" step="1">
  </div>
  <div class="preview" aria-live="polite"></div>
  <code id="hexOut">#FF8000</code>
</div>

<style>
.color-mixer { display: grid; gap: 1rem; max-width: 400px; font-family: monospace; }
.channel { display: grid; gap: 0.25rem; align-items: center; grid-template-columns: auto 1fr; }
.channel label { display: contents; }
.channel output { text-align: right; min-width: 3ch; }
input[type="range"] { accent-color: currentColor; } /* 現代瀏覽器原生主題色 */
#r { accent-color: #ff4444; } #g { accent-color: #44cc44; } #b { accent-color: #4488ff; }
.preview { aspect-ratio: 4/1; border-radius: 8px; border: 1px solid #ddd; transition: background 0.1s; }
#hexOut { display: block; margin-top: 0.5rem; color: #666; }
</style>

<script>
const r = document.getElementById('r'), g = document.getElementById('g'), b = document.getElementById('b');
const ro = document.querySelector('output[for="r"]'), go = document.querySelector('output[for="g"]'), bo = document.querySelector('output[for="b"]');
const preview = document.querySelector('.preview'), hexOut = document.getElementById('hexOut');

function update() {
  const rv = +r.value, gv = +g.value, bv = +b.value;
  ro.value = rv; go.value = gv; bo.value = bv;
  const hex = '#' + [rv, gv, bv].map(v => v.toString(16).padStart(2, '0')).join('');
  preview.style.background = `rgb(${rv}, ${gv}, ${bv})`;
  hexOut.textContent = hex.toUpperCase();
}
[r, g, b].forEach(el => el.addEventListener('input', update));
update();
</script>

樣式客製化:跨瀏覽器統一外觀(純 CSS)

<style>
/* 重置預設外觀 */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  width: 100%;
  height: 40px; /* 擴大點擊區域 */
  cursor: pointer;
}

/* 軌道 */
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px; border-radius: 3px; background: #e0e0e0;
}
input[type="range"]::-moz-range-track {
  height: 6px; border-radius: 3px; background: #e0e0e0;
}

/* 滑塊 */
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px; height: 20px; border-radius: 50%;
  background: #3498db; border: 2px solid white; box-shadow: 0 2px 4px rgba(0,0,0,0.2);
  margin-top: -7px; /* (thumb高 - track高)/2 */
  transition: transform 0.15s, box-shadow 0.15s;
}
input[type="range"]::-moz-range-thumb {
  width: 20px; height: 20px; border-radius: 50%;
  background: #3498db; border: 2px solid white; box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* 狀態 */
input[type="range"]:hover::-webkit-slider-thumb { transform: scale(1.2); box-shadow: 0 4px 8px rgba(0,0,0,0.3); }
input[type="range"]:active::-webkit-slider-thumb { transform: scale(1.3); }
input[type="range"]:focus-visible { outline: none; }
input[type="range"]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 3px #3498db44; }
input[type="range"]:disabled { opacity: 0.5; cursor: not-allowed; }

/* 填充軌道進度(需 JS 配合) */
input[type="range"].styled::-webkit-slider-runnable-track {
  background: linear-gradient(90deg, #3498db var(--value-percent, 0%), #e0e0e0 0%);
}
</style>

<script>
// 同步 --value-percent CSS 變數
document.querySelectorAll('input[type="range"].styled').forEach(el => {
  el.style.setProperty('--value-percent', `${(el.value - el.min) * 100 / (el.max - el.min)}%`);
  el.addEventListener('input', () => el.style.setProperty('--value-percent', `${(el.value - el.min) * 100 / (el.max - el.min)}%`));
});
</script>

垂直滑桿實現

<style>
.vertical-slider {
  writing-mode: bt-lr; /* Chrome/Edge/Safari */
  -webkit-appearance: slider-vertical; /* 非標準但廣泛支援 */
  width: 40px; height: 200px; /* 寬高互換 */
}
</style>
<input type="range" class="vertical-slider" min="0" max="100" value="50">

無障礙增強:<datalist> 刻度標記

<input type="range" id="rating" min="1" max="5" step="1" list="ratingTicks">
<datalist id="ratingTicks">
  <option value="1" label="極差"></option>
  <option value="2" label="差"></option>
  <option value="3" label="普通"></option>
  <option value="4" label="好"></option>
  <option value="5" label="優秀"></option>
</datalist>

螢幕閱讀器會朗讀標籤;視覺上瀏覽器可能在軌道顯示刻度點(支援度不一)。

常見坑點

  • step="any" 允許浮點數,但 value 必須符合步進,否則瀏覽器會自動修正
  • min > max 會導致無效狀態,表單驗證失敗
  • 動態修改 min/max/step 時,value 可能超出範圍,需手動 clamp
  • Safari < 15.4 不支援 accent-color,需偽元素客製化

總結

<input type="range"> 是表單控件中「原生能力最強、客製化最靈活、無障礙最完善」的元素之一。掌握屬性組合、CSS 偽元素客製化、JS 雙向綁定與無障礙增強,即可構建出媲美原生 App 的滑桿介面,從音量控制到複雜參數調節、資料視覺化篩選,皆宜採用。

copyright © 萬盛學電腦網 all rights reserved