1. 程式人生 > >使用CSS美化Chrome下的滾動條樣式

使用CSS美化Chrome下的滾動條樣式

瀏覽器原生的滾動條有時過多會影響介面美觀,IE下的滾動條更是如此,有時我們需要美化一下滾動條,可以使用瀏覽器原生的樣式,或者使用JS外掛,這裡介紹如何修改Chrome下的滾動條樣式。

主要藉助偽元素實現:

//index.html
<body>
    <div class="container">
        <p>Pellentesque habitant morbi tristique senectus...(很多文字)</p>
    </div>
</body>
//style.css
.container {
    width
: 300px; height: 100px; overflow: auto; } p { width: 500px; height: 200px; } ::-webkit-scrollbar { width: 10px; height: 10px; /*background-color: #ddd;*/ } /*滑塊*/ ::-webkit-scrollbar-thumb { background-color: #333; border-radius: 10px; } ::-webkit-scrollbar-thumb:hover { background-color: #777
;
} /*滑道*/ ::-webkit-scrollbar-track { box-shadow: inset 0 0 6px #333; border-radius: 10px; }

執行效果:
這裡寫圖片描述

瀏覽器預設情況下上下左右各有一個按鈕用來滾動,我們也可以自定義圖片來代替它:

//style.css
//上述程式碼
...
/*上下左右按鈕*/
::-webkit-scrollbar-button {
    /*縱方向按鈕的高度,寬度由scrollbar定義*/
    height: 20px;
    /*橫方向按鈕的高度,高度由scrollbar定義*/
    width: 20px;
} ::-webkit-scrollbar-button:vertical:start { background-image: url("up.png"), -webkit-linear-gradient(left, #f9f9f9 50%, #e2e2e2 100%); background-repeat: no-repeat; background-position: bottom left, 0 0; } ::-webkit-scrollbar-button:vertical:end { background-image: url("down.png"), -webkit-linear-gradient(left, #f9f9f9 50%, #e2e2e2 100%); background-repeat: no-repeat; background-position: bottom left, 0 0; } ::-webkit-scrollbar-button:horizontal:start { background-image: url("left.png"), -webkit-linear-gradient(left, #f9f9f9 50%, #e2e2e2 100%); background-repeat: no-repeat; background-position: bottom left, 0 0; } ::-webkit-scrollbar-button:horizontal:end { background-image: url("right.png"), -webkit-linear-gradient(left, #f9f9f9 50%, #e2e2e2 100%); background-repeat: no-repeat; background-position: bottom left, 0 0; }

執行效果:
這裡寫圖片描述

使用JS外掛的話可以上gayhub查詢相關專案,在此列舉2個: