我使用的
<style> .innerbox{ overflow-y: auto; background-color: #f8f8f8; height: 200px; padding: 10px; } .innerbox::-webkit-scrollbar {/*滾動條整體樣式*/ width: 4px; /*高寬分別對應橫豎滾動條的尺寸*/ height: 4px; scrollbar-arrow-color:red; } .innerbox::-webkit-scrollbar-thumb {/*滾動條里面小方塊*/ border-radius: 5px; -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2); background: rgba(0,0,0,0.2); scrollbar-arrow-color:red; } .innerbox::-webkit-scrollbar-track {/*滾動條里面軌道*/ -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2); border-radius: 0; background: rgba(0,0,0,0.1); } </style>
http://www.php.cn/css-tutorial-381391.html
首先我們要了解滾動條。滾動條從外觀來看是由兩部分組成:1,可以滑動的部分,我們叫它滑塊2,滾動條的軌道,即滑塊的軌道,一般來說滑塊的顏色比軌道的顏色深。
滾動條的css樣式主要有三部分組成:
1、::-webkit-scrollbar 定義了滾動條整體的樣式;
2、::-webkit-scrollbar-thumb 滑塊部分;
3、::-webkit-scrollbar-thumb 軌道部分;
下面以overflow-y:auto;為例(overflow-x:auto同)
html代碼:
<
p
class
=
"test test-1"
>
<
p
class
=
"scrollbar"
></
p
>
</
p
>
css代碼:
.test{
width
:
50px
;
height
:
200px
;
overflow
:
auto
;
float
:
left
;
margin
:
5px
;
border
:
none
;
}
.scrollbar{
width
:
30px
;
height
:
300px
;
margin
:
0
auto
;
}
.test
-1:
:-webkit-scrollbar {
/*滾動條整體樣式*/
width
:
10px
;
/*高寬分別對應橫豎滾動條的尺寸*/
height
:
1px
;
}
.test
-1:
:-webkit-scrollbar-thumb {
/*滾動條里面小方塊*/
border-radius:
10px
;
-webkit-box-shadow:
inset
0
0
5px
rgba(
0
,
0
,
0
,
0.2
);
background
:
#535353
;
}
.test
-1:
:-webkit-scrollbar-track {
/*滾動條里面軌道*/
-webkit-box-shadow:
inset
0
0
5px
rgba(
0
,
0
,
0
,
0.2
);
border-radius:
10px
;
background
:
#EDEDED
;
}
效果如下如:
如果要改變滾動條的寬度:在::-webkit-scrollbar中改變即可;如果要改變滾動條滑塊的圓角,在::-webkit-scrollbar-thumb 中改變;如果要改軌道的圓角在::-webkit-scrollbar-track中改變;
此外,滾動條的滑塊不僅可以填充顏色還可以填充圖片如下:
css代碼:
.test
-5:
:-webkit-scrollbar {
/*滾動條整體樣式*/
width
:
10px
;
/*高寬分別對應橫豎滾動條的尺寸*/
height
:
1px
;
}
.test
-5:
:-webkit-scrollbar-thumb {
/*滾動條里面小方塊*/
border-radius:
10px
;
background-color
:
#F90
;
background-image
: -webkit-linear-gradient(
45
deg, 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
);
}
.test
-5:
:-webkit-scrollbar-track {
/*滾動條里面軌道*/
-webkit-box-shadow:
inset
0
0
5px
rgba(
0
,
0
,
0
,
0.2
);
/*border-radius: 10px;*/
background
:
#EDEDED
;
}
html代碼:
<
p
class
=
"test test-5"
>
<
p
class
=
"scrollbar"
></
p
>
</
p
>
效果如下:
總結:
通過上文中的實例學習相信小伙伴們就可以做出自己喜歡的滾動條了、如果文檔中會有多個滾動條出現,而且希望所有的滾動條樣式是一樣的,那么偽元素前面不需要加上class、id、標簽名等之類的任何東西。