我使用的
<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、标签名等之类的任何东西。