国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > CSS > 正文

詳解如何自定義CSS滾動條的樣式

2024-07-11 08:59:40
字體:
來源:轉載
供稿:網友

本文會介紹CSS滾動條選擇器,并在demo中展示如何在Webkit內核瀏覽器和IE瀏覽器中,自定義一個橫向以及一個縱向的滾動條。

0.需求

有的時候我們不想使用瀏覽器默認的滾動條樣式,因為不夠定制化和美觀。那么如何自定義滾動條的樣式呢?下面一起來看看吧。

1 基礎知識

1.1 Webkit內核的css滾動條選擇器

::-webkit-scrollbar CSS偽類選擇器影響了一個元素的滾動條的樣式

屬性:

::-webkit-scrollbar — 整個滾動條

::-webkit-scrollbar-track — 滾動條軌道

::-webkit-scrollbar-thumb — 滾動條上的滾動滑塊

::-webkit-scrollbar-button — 滾動條上的按鈕 (上下箭頭)

::-webkit-scrollbar-track-piece — 滾動條沒有滑塊的軌道部分

::-webkit-scrollbar-corner — 邊角,即當同時有垂直滾動條和水平滾動條時交匯的部分

::-webkit-resizer — 某些元素的corner部分的部分樣式(例:textarea的可拖動按鈕)

注意:

(1)瀏覽器的支持情況:

::-webkit-scrollbar 僅僅在支持Webkit的瀏覽器 (Chrome, Safari)可以使用。

(2)可設置豎直/水平方向的滾動條

可以設置水平方向的滾動條(:horizontal),不加默認是豎直方向(:vertical)。

(3)滾動條上的按鈕(:decrement、:increment)

可以設置圖片,這點會在下面demo中展示。

1.2 IE自定義滾動條樣式

可自定義的樣式比較少,只能控制滾動條各個部分顯示的顏色,定制性較低。這里我只列舉了部分樣式,諸如scrollbar-3dlight-color、scrollbar-highlight-color等樣式試了下沒有效果,這里不再列出:

scrollbar-arrow-color — 滾動條三角箭頭的顏色 scrollbar-face-color — 滾動條上滾動滑塊顏色

scrollbar-track-color— 滾動條軌道、按鈕背景的顏色 scrollbar-shadow-color— 滾動框上滑塊邊框的顏色

2.demo快速上手

2.1 Webkit內核的瀏覽器自定義滾動條樣式 (chrome, safari)

如果覺得上述說明有些抽象,可以直接在瀏覽器中打開demo,結合demo中的注釋來理解各個屬性的意義。圖中我對其中的一些屬性做了標注,滾動條外層軌道屬性并未在圖中標注,可打開chrome瀏覽器控制臺查看屬性:

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>scrollbar的demo--lynnshen</title>  <style type="text/css">    * {      margin: 0;      padding: 0;    }    .scolltable {      width: 500px;      height: 300px;      border: 1px solid black;            /*實現水平垂直居中*/      position: absolute;      left: 50%;      top: 50%;      margin-left: -250px;      margin-top: -150px;      overflow: scroll;    }    .content {      /*要設的比.scolltable更寬一些*/      width: 600px;    }    /*整個滾動條*/    ::-webkit-scrollbar {      width: 24px;      background-color: transparent;    }    /*水平的整個滾動條*/    ::-webkit-scrollbar:horizontal {      height: 24px;      background-color: transparent;    }    /*滾動條軌道*/    ::-webkit-scrollbar-track {      background-color: #f6f8fc;      border-right: 1px solid #f1f5fa;      border: 1px solid #f1f5fa;      ;    }    /*豎直的滑塊*/    ::-webkit-scrollbar-thumb {      background-color: rgba(220, 228, 243, 1);      border-radius: 0px;      border-top: 1px solid #edf2f9;      border-bottom: 1px solid #edf2f9;      border-left: 1px solid #f1f5fa;    }    /*水平的滑塊*/    ::-webkit-scrollbar-thumb:horizontal {      /* background-color: rgba(220, 228, 243, 1); */      border-radius: 0px;      border-top: 1px solid #edf2f9;      /* border-right: 1px solid #f1f5fa;      border-left: 1px solid #f1f5fa; */    }    /*滾動條上的按鈕--豎直滾動條向上*/    ::-webkit-scrollbar-button:decrement {      border-bottom: 1px solid #edf2f9;      height: 26px;      background: url("./images/scroll_up.png") 7px 9px no-repeat;      border-right: 1px solid #f1f5fa;      border-left: 1px solid #f1f5fa;    }    /*滾動條上的按鈕--豎直滾動條向下*/    ::-webkit-scrollbar-button:increment {      border-top: 1px solid #edf2f9;      height: 26px;      background: url("./images/scroll_down.png") 7px 10px no-repeat;      border-right: 1px solid #f1f5fa;      border-left: 1px solid #f1f5fa;      border-bottom: 1px solid #f1f5fa;    }    /*滾動條上的按鈕--水平滾動條向左*/    ::-webkit-scrollbar-button:horizontal:decrement {      border-top: 1px solid #edf2f9;      width: 26px;      background: url("./images/scroll_left.png") 9px 7px no-repeat;      border-top: 1px solid #f1f5fa;      border-bottom: 1px solid #f1f5fa;      border-right:1px solid #f1f5fa;    }    /*滾動條上的按鈕--水平滾動條向右*/    ::-webkit-scrollbar-button:horizontal:increment {      border-top: 1px solid #edf2f9;      width: 25px;      background: url("./images/scroll_right.png") 10px 7px no-repeat;      border-left:1px solid #f1f5fa;    }        /*邊角*/    ::-webkit-scrollbar-corner{      border:1px solid #dce4f3;    }  </style>  </head><body>  <div class="scolltable">    <div class="content">      內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容 內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容      內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容 內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容      內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容 內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容      內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容    </div>  </div></body></html>

實現效果:

WebKit內核的瀏覽器

說明:

(1)滾動條兩端的按鈕使用的圖片,四個角分別使用了四張圖片;

(2).scolltable實現了水平垂直居中的效果,具體方法是:使用絕對對位,將元素的定點定位到body的中心。然后使用負margin(即元素寬高的一半)將其拉回到body的中心。

2.2 IE自定義滾動條樣式

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>scrollbar for IE--lynnshen</title>  <style type="text/css">    * {      margin: 0;      padding: 0;    }    .scolltable {      width: 500px;      height: 300px;      border: 1px solid black;            /*實現水平垂直居中*/      position: absolute;      left: 50%;      top: 50%;      margin-left: -250px;      margin-top: -150px;      overflow: scroll;            scrollbar-face-color:greenyellow;      scrollbar-arrow-color:goldenrod;      scrollbar-shadow-color:red;      scrollbar-track-color:pink;    }    .content {      /*要設的比.scolltable更寬一些*/      width: 600px;    }      </style>  </head><body>  <div class="scolltable">    <div class="content">      內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容 內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容      內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容 內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容      內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容 內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容      內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容    </div>  </div></body></html>

實現效果:

IE

3.小結

本文主要是想記錄下在Webkit內核的瀏覽器和IE中,如何自定義滾動條的樣式,并分別提供了兩個demo。如有問題,歡迎指正。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普格县| 汤原县| 甘谷县| 南华县| 赞皇县| 德庆县| 黑龙江省| 孟津县| 两当县| 芒康县| 拉孜县| 玉溪市| 茶陵县| 宜黄县| 双城市| 阿瓦提县| 巩留县| 石渠县| 泰宁县| 鄂托克前旗| 长兴县| 中西区| 山西省| 彰武县| 邻水| 甘肃省| 巨鹿县| 星子县| 麻城市| 庆城县| 镇沅| 青铜峡市| 佳木斯市| 黑山县| 蓬溪县| 乐平市| 潜江市| 潜江市| 兴化市| 晋宁县| 鄂托克旗|