::-webkit-scrollbar
非標準:此特性未標準化。我們不建議在生產環境中使用非標準特性,因為它們瀏覽器支援有限,並且可能會更改或被移除。但是,在沒有標準選項的特定情況下,它們可以是合適的替代方案。
::-webkit-scrollbar CSS 偽元素影響元素在具有可滾動溢位時的捲軸樣式。
對於不支援此偽元素及相關 ::-webkit-scrollbar-* 偽元素的瀏覽器,可以使用標準的 scrollbar-color 和 scrollbar-width 屬性作為替代方案(參閱瀏覽器相容性)。
注意:如果 scrollbar-color 和 scrollbar-width 受到支援,並且設定了除 auto 之外的任何值,它們將覆蓋 ::-webkit-scrollbar-* 樣式。有關詳細資訊,請參閱為捲軸樣式新增備用方案。
CSS 捲軸選擇器
你可以使用以下偽元素來自定義 WebKit 瀏覽器捲軸的各個部分:
::-webkit-scrollbar— 整個捲軸。::-webkit-scrollbar-button— 捲軸上的按鈕(向上和向下指向的箭頭,每次滾動一行)。::-webkit-scrollbar:horizontal— 水平捲軸。::-webkit-scrollbar-thumb— 可拖動的滾動手柄。::-webkit-scrollbar-track— 捲軸的軌道(進度條),上面有一條灰色條,下面有一條白色條。::-webkit-scrollbar-track-piece— 軌道(進度條)中未被手柄覆蓋的部分。::-webkit-scrollbar:vertical— 垂直捲軸。::-webkit-scrollbar-corner— 捲軸的底部角落,水平和垂直捲軸在此處相遇。這通常是瀏覽器視窗的右下角。::-webkit-resizer— 出現在某些元素底角的,可拖動的調整大小手柄。
無障礙
開發者應避免樣式化捲軸,因為改變捲軸的外觀使其偏離預設樣式會破壞外部一致性,從而對可用性產生負面影響。如果確實要樣式化捲軸,請確保顏色對比度足夠高,並且觸控目標至少為 44px 寬和高。請參閱 WCAG 2.0 技術:G183:使用 3:1 的對比度 和 理解 WCAG 2.1:目標大小。
示例
使用 -webkit-scrollbar 樣式化捲軸
CSS
.visible-scrollbar,
.invisible-scrollbar,
.mostly-customized-scrollbar {
display: block;
width: 10em;
overflow: auto;
height: 2em;
padding: 1em;
margin: 1em auto;
outline: 2px dashed cornflowerblue;
}
.invisible-scrollbar::-webkit-scrollbar {
display: none;
}
/* Demonstrate a "mostly customized" scrollbar
* (won't be visible otherwise if width/height is specified) */
.mostly-customized-scrollbar::-webkit-scrollbar {
width: 5px;
height: 8px;
background-color: #aaaaaa; /* or add it to the track */
}
/* Add a thumb */
.mostly-customized-scrollbar::-webkit-scrollbar-thumb {
background: black;
}
HTML
<div class="visible-scrollbar">
<h3>Visible scrollbar</h3>
<p>
Etiam sagittis sem sed lacus laoreet, eu fermentum eros auctor. Proin at
nulla elementum, consectetur ex eget, commodo ante. Sed eros mi, bibendum ut
dignissim et, maximus eget nibh. Phasellus blandit quam turpis, at mollis
velit pretium ut. Nunc consequat efficitur ultrices. Nullam hendrerit
posuere est. Nulla libero sapien, egestas ac felis porta, cursus ultricies
quam. Vestibulum tincidunt accumsan sapien, a fringilla dui semper in.
Vivamus consectetur ipsum a ornare blandit. Aenean tempus at lorem sit amet
faucibus. Curabitur nibh justo, faucibus sed velit cursus, mattis cursus
dolor. Pellentesque id pretium est. Quisque convallis nisi a diam malesuada
mollis. Aliquam at enim ligula.
</p>
</div>
<div class="invisible-scrollbar">
<h3>Invisible scrollbar</h3>
<p>
Thisisaveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerylongword
</p>
</div>
<div class="mostly-customized-scrollbar">
<h3>Custom scrollbar</h3>
<p>
Thisisaveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerylongword<br />
And pretty tall<br />
thing with weird scrollbars.<br />
Who thought scrollbars could be made weird?
</p>
</div>
結果
為捲軸樣式新增備用方案
你可以使用 @supports at-rule 來檢測瀏覽器是否支援標準的 scrollbar-color 和 scrollbar-width 屬性,否則使用 ::-webkit-scrollbar-* 偽元素作為備用方案。以下示例展示瞭如何在支援時使用 scrollbar-color 為捲軸應用顏色,如果不支援則使用 ::-webkit-scrollbar-* 偽元素。
HTML
<div class="scroll-box">
<h1>Yoshi</h1>
<p>
Yoshi is a fictional dinosaur who appears in video games published by
Nintendo. Yoshi debuted in Super Mario World (1990) on the SNES as Mario and
Luigi's sidekick.
</p>
<p>
Throughout the mainline Super Mario series, Yoshi typically serves as
Mario's trusted steed.
</p>
<p>
With a gluttonous appetite, Yoshi can gobble enemies with his long tongue,
and lay eggs that doubly function as projectiles.
</p>
</div>
CSS
/* For browsers that support `scrollbar-*` properties */
@supports (scrollbar-color: auto) {
.scroll-box {
scrollbar-color: aquamarine cornflowerblue;
}
}
/* Otherwise, use `::-webkit-scrollbar-*` pseudo-elements */
@supports selector(::-webkit-scrollbar) {
.scroll-box::-webkit-scrollbar {
background: aquamarine;
}
.scroll-box::-webkit-scrollbar-thumb {
background: cornflowerblue;
}
}
結果
在下面的示例中,你可以垂直滾動帶邊框的盒子以檢視捲軸樣式化的效果。
規範
不屬於任何標準。
瀏覽器相容性
css.selectors.-webkit-scrollbar
載入中…
css.selectors.-webkit-scrollbar-button
載入中…
css.selectors.-webkit-scrollbar-thumb
載入中…
css.selectors.-webkit-scrollbar-track
載入中…
css.selectors.-webkit-scrollbar-track-piece
載入中…
css.selectors.-webkit-scrollbar-corner
載入中…
css.selectors.-webkit-resizer
載入中…
另見
scrollbar-widthscrollbar-color- 不要使用自定義捲軸 (2023)
- 開發者 Chrome 上的捲軸樣式 (2024)
- WebKit.org 上的捲軸樣式 (2009)