CSS3自定義滾動條樣式 -webkit-scrollbar


前言

webkit支持擁有overflow屬性的區域,列表框,下拉菜單,textarea的滾動條自定義樣式,所以用處還是挺大的。當然,兼容所有瀏覽器的滾動條樣式目前是不存在的。

演示

來看看這2個滾動條demo: demo1(圖片版)demo2(純CSS3版)

滾動條組成

  • ::-webkit-scrollbar 滾動條整體部分
  • ::-webkit-scrollbar-thumb  滾動條里面的小方塊,能向上向下移動(或往左往右移動,取決於是垂直滾動條還是水平滾動條)
  • ::-webkit-scrollbar-track  滾動條的軌道(里面裝有Thumb)
  • ::-webkit-scrollbar-button 滾動條的軌道的兩端按鈕,允許通過點擊微調小方塊的位置。
  • ::-webkit-scrollbar-track-piece 內層軌道,滾動條中間部分(除去)
  • ::-webkit-scrollbar-corner 邊角,即兩個滾動條的交匯處
  • ::-webkit-resizer 兩個滾動條的交匯處上用於通過拖動調整元素大小的小控件

簡潔版

這里就不貼出詳細代碼了,demo里面可以通過查看源碼尋找具體樣式的設置。來看看demo2中第二個滾動條的樣式

 

[css]  view plain  copy
 
  1. /*定義滾動條高寬及背景 高寬分別對應橫豎滾動條的尺寸*/  
  2. ::-webkit-scrollbar  
  3. {  
  4.     width: 16px;  
  5.     height: 16px;  
  6.     background-color: #F5F5F5;  
  7. }  
  8.   
  9. /*定義滾動條軌道 內陰影+圓角*/  
  10. ::-webkit-scrollbar-track  
  11. {  
  12.     -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);  
  13.     border-radius: 10px;  
  14.     background-color: #F5F5F5;  
  15. }  
  16.   
  17. /*定義滑塊 內陰影+圓角*/  
  18. ::-webkit-scrollbar-thumb  
  19. {  
  20.     border-radius: 10px;  
  21.     -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);  
  22.     background-color: #555;  
  23. }  

 

詳細設置

 

定義滾動條就是利用偽元素與偽類,那什么是偽元素和偽類呢?

偽類大家應該很熟悉:link,:focus,:hover,此外CSS3中又增加了許多偽類選擇器,如:nth-child,:last-child,:nth-last-of-type()等。

CSS中的偽元素大家以前看過::first-line,:first-letter,:before,:after。那么在css3中,偽元素進行了調整,在以前的基礎上增加了一個“:”也就是現在變成了“::first-letter,::first-line,::before,::after”,另外CSS3還增加了一個“::selection”。兩個“::”和一個“:”在css3中主要用來區分偽類和偽元素。

webkit的偽類和偽元素的實現很強,可以把滾動條當成一個頁面元素來定義,再結合一些高級的CSS3屬性,比如漸變、圓角、RGBa等等。然后如果有些地方要用圖片,可以把圖片也可以轉換成Base64,不然每次都得加載那個多個圖片,增加請求數。

任何對象都可以設置:邊框、陰影、背景圖片等等,創建的滾動條任然會按照操作系統本身的設置來完成其交互的行為。下面的偽類可以應用到上面的偽元素中。有點小復雜,具體怎么寫可以看第一個demo,那里也有注釋。

[css]  view plain  copy
 
  1. :horizontal  
  2. //horizontal偽類適用於任何水平方向上的滾動條  
  3.   
  4. :vertical  
  5. //vertical偽類適用於任何垂直方向的滾動條  
  6.   
  7. :decrement  
  8. //decrement偽類適用於按鈕和軌道碎片。表示遞減的按鈕或軌道碎片,例如可以使區域向上或者向右移動的區域和按鈕  
  9.   
  10. :increment  
  11. //increment偽類適用於按鈕和軌道碎片。表示遞增的按鈕或軌道碎片,例如可以使區域向下或者向左移動的區域和按鈕  
  12.   
  13. :start  
  14. //start偽類適用於按鈕和軌道碎片。表示對象(按鈕 軌道碎片)是否放在滑塊的前面  
  15.   
  16. :end  
  17. //end偽類適用於按鈕和軌道碎片。表示對象(按鈕 軌道碎片)是否放在滑塊的后面  
  18.   
  19. :double-button  
  20. //double-button偽類適用於按鈕和軌道碎片。判斷軌道結束的位置是否是一對按鈕。也就是軌道碎片緊挨着一對在一起的按鈕。  
  21.   
  22. :single-button  
  23. //single-button偽類適用於按鈕和軌道碎片。判斷軌道結束的位置是否是一個按鈕。也就是軌道碎片緊挨着一個單獨的按鈕。  
  24.   
  25. :no-button  
  26. no-button偽類表示軌道結束的位置沒有按鈕。  
  27.   
  28. :corner-present  
  29. //corner-present偽類表示滾動條的角落是否存在。  
  30.   
  31. :window-inactive  
  32. //適用於所有滾動條,表示包含滾動條的區域,焦點不在該窗口的時候。  
  33.   
  34. ::-webkit-scrollbar-track-piece:start {  
  35. /*滾動條上半邊或左半邊*/  
  36. }  
  37.   
  38. ::-webkit-scrollbar-thumb:window-inactive {  
  39. /*當焦點不在當前區域滑塊的狀態*/  
  40. }  
  41.   
  42. ::-webkit-scrollbar-button:horizontal:decrement:hover {  
  43. /*當鼠標在水平滾動條下面的按鈕上的狀態*/  
  44. }  
 
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS3自定義滾動條-軒楓閣</title>
<style>
header
{
    font-family: 'Lobster', cursive;
    text-align: center;
    font-size: 25px;    
}

#info
{
    font-size: 18px;
    color: #555;
    text-align: center;
    margin-bottom: 25px;
}

a{
    color: #074E8C;
}

.scrollbar
{
    margin-left: 30px;
    float: left;
    height: 300px;
    width: 65px;
    background: #F5F5F5;
    overflow-y: scroll;
    margin-bottom: 25px;
}

.force-overflow
{
    min-height: 450px;
}

#wrapper
{
    text-align: center;
    width: 500px;
    margin: auto;
}

/*
 *  STYLE 1
 */

#style-1::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    border-radius: 10px;
    background-color: #F5F5F5;
}

#style-1::-webkit-scrollbar
{
    width: 12px;
    background-color: #F5F5F5;
}

#style-1::-webkit-scrollbar-thumb
{
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
    background-color: #555;
}

/*
 *  STYLE 2
 */

#style-2::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    border-radius: 10px;
    background-color: #F5F5F5;
}

#style-2::-webkit-scrollbar
{
    width: 12px;
    background-color: #F5F5F5;
}

#style-2::-webkit-scrollbar-thumb
{
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
    background-color: #D62929;
}

/*
 *  STYLE 3
 */

#style-3::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    background-color: #F5F5F5;
}

#style-3::-webkit-scrollbar
{
    width: 6px;
    background-color: #F5F5F5;
}

#style-3::-webkit-scrollbar-thumb
{
    background-color: #000000;
}

/*
 *  STYLE 4
 */

#style-4::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    background-color: #F5F5F5;
}

#style-4::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-4::-webkit-scrollbar-thumb
{
    background-color: #000000;
    border: 2px solid #555555;
}


/*
 *  STYLE 5
 */

#style-5::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    background-color: #F5F5F5;
}

#style-5::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-5::-webkit-scrollbar-thumb
{
    background-color: #0ae;
    
    background-image: -webkit-gradient(linear, 0 0, 0 100%,
                       color-stop(.5, rgba(255, 255, 255, .2)),
                       color-stop(.5, transparent), to(transparent));
}


/*
 *  STYLE 6
 */

#style-6::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    background-color: #F5F5F5;
}

#style-6::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-6::-webkit-scrollbar-thumb
{
    background-color: #F90;    
    background-image: -webkit-linear-gradient(45deg,
                                              rgba(255, 255, 255, .2) 25%,
                                              transparent 25%,
                                              transparent 50%,
                                              rgba(255, 255, 255, .2) 50%,
                                              rgba(255, 255, 255, .2) 75%,
                                              transparent 75%,
                                              transparent)
}


/*
 *  STYLE 7
 */

#style-7::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    background-color: #F5F5F5;
    border-radius: 10px;
}

#style-7::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-7::-webkit-scrollbar-thumb
{
    border-radius: 10px;
    background-image: -webkit-gradient(linear,
                                       left bottom,
                                       left top,
                                       color-stop(0.44, rgb(122,153,217)),
                                       color-stop(0.72, rgb(73,125,189)),
                                       color-stop(0.86, rgb(28,58,148)));
}

/*
 *  STYLE 8
 */

#style-8::-webkit-scrollbar-track
{
    border: 1px solid black;
    background-color: #F5F5F5;
}

#style-8::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-8::-webkit-scrollbar-thumb
{
    background-color: #000000;    
}


/*
 *  STYLE 9
 */

#style-9::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    background-color: #F5F5F5;
}

#style-9::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-9::-webkit-scrollbar-thumb
{
    background-color: #F90;    
    background-image: -webkit-linear-gradient(90deg,
                                              rgba(255, 255, 255, .2) 25%,
                                              transparent 25%,
                                              transparent 50%,
                                              rgba(255, 255, 255, .2) 50%,
                                              rgba(255, 255, 255, .2) 75%,
                                              transparent 75%,
                                              transparent)
}


/*
 *  STYLE 10
 */

#style-10::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    background-color: #F5F5F5;
    border-radius: 10px;
}

#style-10::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-10::-webkit-scrollbar-thumb
{
    background-color: #AAA;
    border-radius: 10px;
    background-image: -webkit-linear-gradient(90deg,
                                              rgba(0, 0, 0, .2) 25%,
                                              transparent 25%,
                                              transparent 50%,
                                              rgba(0, 0, 0, .2) 50%,
                                              rgba(0, 0, 0, .2) 75%,
                                              transparent 75%,
                                              transparent)
}


/*
 *  STYLE 11
 */

#style-11::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
    background-color: #F5F5F5;
    border-radius: 10px;
}

#style-11::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-11::-webkit-scrollbar-thumb
{
    background-color: #3366FF;
    border-radius: 10px;
    background-image: -webkit-linear-gradient(0deg,
                                              rgba(255, 255, 255, 0.5) 25%,
                                              transparent 25%,
                                              transparent 50%,
                                              rgba(255, 255, 255, 0.5) 50%,
                                              rgba(255, 255, 255, 0.5) 75%,
                                              transparent 75%,
                                              transparent)
}

/*
 *  STYLE 12
 */

#style-12::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.9);
    border-radius: 10px;
    background-color: #444444;
}

#style-12::-webkit-scrollbar
{
    width: 12px;
    background-color: #F5F5F5;
}

#style-12::-webkit-scrollbar-thumb
{
    border-radius: 10px;
    background-color: #D62929;
    background-image: -webkit-linear-gradient(90deg,
                                              transparent,
                                              rgba(0, 0, 0, 0.4) 50%,
                                              transparent,
                                              transparent)
}

/*
 *  STYLE 13
 */

#style-13::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.9);
    border-radius: 10px;
    background-color: #CCCCCC;
}

#style-13::-webkit-scrollbar
{
    width: 12px;
    background-color: #F5F5F5;
}

#style-13::-webkit-scrollbar-thumb
{
    border-radius: 10px;
    background-color: #D62929;
    background-image: -webkit-linear-gradient(90deg,
                                              transparent,
                                              rgba(0, 0, 0, 0.4) 50%,
                                              transparent,
                                              transparent)
}

/*
 *  STYLE 14
 */

#style-14::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.6);
    background-color: #CCCCCC;
}

#style-14::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-14::-webkit-scrollbar-thumb
{
    background-color: #FFF;
    background-image: -webkit-linear-gradient(90deg,
                                              rgba(0, 0, 0, 1) 0%,
                                              rgba(0, 0, 0, 1) 25%,
                                              transparent 100%,
                                              rgba(0, 0, 0, 1) 75%,
                                              transparent)
}

/*
 *  STYLE 15
 */

#style-15::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.1);
    background-color: #F5F5F5;
    border-radius: 10px;
}

#style-15::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-15::-webkit-scrollbar-thumb
{
    border-radius: 10px;
    background-color: #FFF;
    background-image: -webkit-gradient(linear,
                                       40% 0%,
                                       75% 84%,
                                       from(#4D9C41),
                                       to(#19911D),
                                       color-stop(.6,#54DE5D))
}

/*
 *  STYLE 16
 */

#style-16::-webkit-scrollbar-track
{
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.1);
    background-color: #F5F5F5;
    border-radius: 10px;
}

#style-16::-webkit-scrollbar
{
    width: 10px;
    background-color: #F5F5F5;
}

#style-16::-webkit-scrollbar-thumb
{
    border-radius: 10px;
    background-color: #FFF;
    background-image: -webkit-linear-gradient(top,
                                              #e4f5fc 0%,
                                              #bfe8f9 50%,
                                              #9fd8ef 51%,
                                              #2ab0ed 100%);
}


</style>
</head>
<body>
      <div id="wrapper">
        <div class="scrollbar" id="style-default">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-1">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-2">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-3">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-4">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-5">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-6">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-7">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-8">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-9">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-10">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-11">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-13">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-14">
          <div class="force-overflow"></div>
        </div>

        <div class="scrollbar" id="style-15">
          <div class="force-overflow"></div>
        </div>
    </div>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM