我如何通过CSS(层叠样式表)为一个div而不是整个页面自定义滚动条?
当前回答
试试吧
来源:https://nicescroll.areaaperta.com/
简单的实现
<script type="text/javascript">
$(document).ready(
function() {
$("html").niceScroll();
}
);
</script>
它是一个jQuery插件滚动条,所以你的滚动条是可控的,在不同的操作系统上看起来是一样的。
其他回答
请检查这个链接。带有工作演示的示例
#style-1::-webkit-scrollbar-track
{
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
border-radius: 10px;
background-color: #F5F5F5;
}
#style-1::-webkit-scrollbar
{
width: 12px;
background-color: #F5F5F5;
}
#style-1::-webkit-scrollbar-thumb
{
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
background-color: #555;
}
CSS滚动条
试试吧
来源:https://nicescroll.areaaperta.com/
简单的实现
<script type="text/javascript">
$(document).ready(
function() {
$("html").niceScroll();
}
);
</script>
它是一个jQuery插件滚动条,所以你的滚动条是可控的,在不同的操作系统上看起来是一样的。
对于使用sass的人来说,这里是一个具有基本功能(拇指,轨道颜色和宽度)的mixin。我没有广泛地测试它,所以请随意指出任何错误。
@mixin element-scrollbar($thumb-color, $background-color: mix($thumb-color, white, 50%), $width: 1rem) {
// For Webkit
&::-webkit-scrollbar-thumb {
background: $thumb-color;
}
&::-webkit-scrollbar-track {
background: $background-color;
}
&::-webkit-scrollbar {
width: $width;
height: $width;
}
// For Internet Explorer
& {
scrollbar-face-color: $thumb-color;
scrollbar-arrow-color: $thumb-color;
scrollbar-track-color: $background-color;
}
// For Firefox future compatibility
// This is W3C draft and does not work yet. Use html-firefox-scrollbar mixin instead.
& {
scrollbar-color: $thumb-color $background-color;
scrollbar-width: $width;
}
}
// For Firefox
@mixin html-firefox-scrollbar($thumb-color, $background-color: mix($thumb-color, white, 50%), $firefox-width: this) {
// This must be used on html/:root element to take effect
& {
scrollbar-color: $thumb-color $background-color;
scrollbar-width: $firefox-width;
}
}
Firefox新版本(64)支持CSS滚动条模块级别1
.scroller { width: 300px; height: 100px; overflow-y: scroll; scrollbar-color: rebeccapurple green; scrollbar-width: thin; } <div class="scroller"> Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi welsh onion daikon amaranth tatsoi tomatillo melon azuki bean garlic. Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini. </div>
来源:https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Scrollbars
https://codepen.io/fatihhayri/pen/pqdrbd
Webkit浏览器(如Chrome, Safari和Opera)支持非标准的::-webkit-scrollbar伪元素,它允许我们修改浏览器滚动条的外观。
注意:Firefox、IE和Edge不支持::-webkit-scrollbar。
* { box-sizing: border-box; font-family: sans-serif; } div { width: 15rem; height: 8rem; padding: .5rem; border: 1px solid #aaa; margin-bottom: 1rem; overflow: auto; } .box::-webkit-scrollbar { width: .8em; } .box::-webkit-scrollbar-track { box-shadow: inset 0 0 6px rgba(0, 0, 0, .3); } .box::-webkit-scrollbar-thumb { background-color: dodgerblue; } <div class="box"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</p> </div> <div> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate</p> </div>
参考: 如何创建自定义滚动条