我使用的
<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.htmlphp
首先咱們要了解滾動條。滾動條從外觀來看是由兩部分組成:1,能夠滑動的部分,咱們叫它滑塊2,滾動條的軌道,即滑塊的軌道,通常來講滑塊的顏色比軌道的顏色深。css
滾動條的css樣式主要有三部分組成:html
一、::-webkit-scrollbar 定義了滾動條總體的樣式;web
二、::-webkit-scrollbar-thumb 滑塊部分;學習
三、::-webkit-scrollbar-thumb 軌道部分;spa
下面以overflow-y:auto;爲例(overflow-x:auto同)code
html代碼:htm
<
p
class
=
"test test-1"
>
blog
<
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、標籤名等之類的任何東西。