DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> WEB網站前端 >> WEB前端代碼
自訂 Chrome 與 IE 捲軸樣式的完整寫法
編輯:WEB前端代碼     

預設的瀏覽器捲軸外觀往往和整體設計格格不入。好消息是,Chrome / Safari 這類使用 WebKit 的瀏覽器可以透過偽元素自訂捲軸,IE 則有一套專屬的 scrollbar-* 屬性。

完整範例

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>捲軸樣式</title>
<style type="text/css" media="screen">
::-webkit-scrollbar {
width: 12px;
}
::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
-webkit-border-radius: 10px;
border-radius: 10px;
}
::-webkit-scrollbar-thumb {
-webkit-border-radius: 10px;
border-radius: 10px;
background: rgba(255,0,0,0.8);
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);
}
::-webkit-scrollbar-thumb:window-inactive {
background: rgba(255,0,0,0.4);
}
.scroll-keleyi-com {
SCROLLBAR-FACE-COLOR: rgb(255,0,0);
SCROLLBAR-3DLIGHT-COLOR: rgb(255,0,0);
SCROLLBAR-DARKSHADOW-COLOR: rgb(255,0,0);
SCROLLBAR-BASE-COLOR: rgb(255,255,255);
width:150px;
height:400px;
overflow-x:hidden;
overflow-y:auto;
border:1px solid green;
}
</style>
</head>
<body>
<div style="width:400px;margin:0px auto;">
<ul class="scroll-keleyi-com">
<li>菜單項目一</li>
<li>菜單項目二</li>
<li>菜單項目三</li>
<li>菜單項目四</li>
<li>菜單項目五</li>
<li>菜單項目六</li>
<li>菜單項目七</li>
<li>菜單項目八</li>
</ul>
</div>
</body>
</html>

WebKit 捲軸的四個關鍵偽元素

::-webkit-scrollbar 控制整體寬度。
::-webkit-scrollbar-track 是軌道(背景槽),常用來放內陰影與圓角。
::-webkit-scrollbar-thumb 是滑桿本體,也就是可以拖曳的部分。
::-webkit-scrollbar-thumb:window-inactive 則對應視窗失焦時的狀態,加上它可以避免切換視窗後顏色顯得突兀。

另外還有 ::-webkit-scrollbar-button(上下箭頭按鈕)、::-webkit-scrollbar-corner(右下角交會處)與 ::-webkit-resizer(縮放把手)可以一併調整。

另一組精簡的 WebKit 寫法

::-webkit-scrollbar { width: 15px; }
::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); border-radius: 3px; }
::-webkit-scrollbar-thumb { border-radius: 3px; -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5); }

IE 專屬屬性

.scrollBar-keleyi-com{
scrollbar-face-color: #B5B5B5; /*滑桿主色*/
scrollbar-highlight-color: #FFFFFF; /*高光*/
scrollbar-shadow-color: #DDD; /*陰影*/
scrollbar-3dlight-color: #DDD; /*左側邊高光*/
scrollbar-arrow-color: #FFF; /*箭頭按鈕顏色*/
scrollbar-track-color: #EEEEEE; /*軌道底色*/
scrollbar-darkshadow-color: #DDD; /*深色陰影*/
}

實務提醒:Firefox 直到較新版本才提供 scrollbar-width 與 scrollbar-color 兩個標準屬性,功能比 WebKit 那套陽春得多。若專案必須跨瀏覽器一致,通常還是得靠自製的 JS 捲軸元件才能完全統一外觀。

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