自定义滚动条样式 -webkit-scrollbar

2021-08-03  本文已影响0人  匆匆那年_海

滚动条的形成:当内容超出容器时,容器可设置overflow:auto;出现滚动条,其自带的滚动条有时无法满足我们审美,我们可以通过css伪类来实现自定义滚动条。

滚动条的组成:1.可以滑动的部分,称它为滑块,2.滚动条的轨道,即滑块的轨道,一般滑块的颜色比轨道的颜色深,从而突出滑块。

如果希望所有的滚动条样式是一样,记得滚动条里面小方块和滚动条轨道样式后面加!important,若要单独定义,给::-webkit前面加上选择器class id等等即可,无需加!important

html
<div class="test">
    <div class="scrollbar"></div>
</div>
css
.test {
    width: 800px;
    height: 200px;
    overflow: auto;
}

.scrollbar {
    width: 1000px;
    height: 300px;
    margin: 0 auto;
}

/*滚动条整体粗细样式*/
::-webkit-scrollbar {
    /*高宽分别对应横竖滚动条的尺寸*/
    width: 8px;
    height: 8px;
}

/*滚动条里面小方块*/
::-webkit-scrollbar-thumb {
    border-radius: 10px !important;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2) !important;
    /* 颜色 */
    /* background:#b6b6b6!important; */
    /* 线性渐变背景 */
    background-image: linear-gradient(45deg, #ffbd61 25%,#ffbd61 25%, #ff8800 25%, #ff8800 50%,#ffbd61 50%, #ffbd61 75%, #ff8800 75%, #ff8800 100%)!important;
}

/*滚动条轨道*/
::-webkit-scrollbar-track {
    border-radius: 10px !important;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2) !important;
    background: #EDEDED !important;
}
滑块背景为颜色 滑块背景为线性渐变

原文作者:匆匆那年_海,博客主页:https://www.jianshu.com/u/910c0667c515
95后前端汉子,爱编程、优秀、聪明、理性、沉稳、智慧的程序猿一枚。

上一篇 下一篇

猜你喜欢

热点阅读