滚动条的样式处理
场景
做需求时根据ui设计图处理的,滚动条在mac上显示正常,但是在windows本上显示不正常,就处理了滚动条的样式,代码如下:
/* 滚动条的整体样式处理 */
.box::-webkit-scrollbar {
/*高宽分别对应横竖滚动条的尺寸*/
width : 10px;
height: 10px;
}
/* 滚动条中的小方块处理 */
.box::-webkit-scrollbar-thumb {
/*滚动条里面小方块*/
border-radius: 10px;
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
background: green;
}
/* 滚动条滚动的轨道 */
.box::-webkit-scrollbar-track {
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
border-radius: 10px;
background: red;
}
后来发现有统一的css样式处理,也体会到了sass的优点,自己通过这个整理的代码如下:
commonStyle.scss
$color: rgb(14, 17, 20);
$raduis: 50px;
// 滚动条的兼容性样式修改
@mixin scrollBar {
&::-webkit-scrollbar {
width: 10px;
height: 10px;
}
&::-webkit-scrollbar-track {
border-radius: $color;
background-color: transparent;
}
&::-webkit-scrollbar-thumb {
border: 3px solid transparent;
border-radius: $raduis;
background-color: rgba($color, 0.1);
background-clip: content-box;
&:hover {
background-color: rgba($color, 0.3);
}
}
}
你目前正在做的样式处理的scss
// 引入整体的scss文件
@import './commonStyle.scss';
$prefixCls: "box";
.#{$prefixCls} {
// 直接使用
@include scrollBar;
overflow: auto;
height: 1000px;
width: 100%;
}
使用sass可以将这些公共的css样式处理写到一个统一的文件中,如果需要使用时引入就可以使用。而不用像css一样每次用到都会重写一遍。(虽然转化后的文件和用css写一遍没有区别,但这样大大提高了效率)