div中的CSS自定义滚动条

标签 css html scrollbar

如何在 CSS(层叠样式表)中为一个 div 而不是整个页面自定义滚动条?

最佳答案

我认为整合有关滚动条、CSS 和浏览器兼容性的最新信息会很有帮助。

滚动条 CSS 支持

目前,不存在跨浏览器滚动条 CSS 样式定义。 The W3C article我在最后提到有以下声明,最近更新(2014 年 10 月 10 日):

Some browsers (IE, Konqueror) support the non-standard properties 'scrollbar-shadow-color', 'scrollbar-track-color' and others. These properties are illegal: they are neither defined in any CSS specification nor are they marked as proprietary (by prefixing them with "-vendor-")

微软

正如其他人所提到的,Microsoft 支持滚动条样式,但仅适用于 IE8 及更高版本。

例子:

.TA {
    scrollbar-3dlight-color:gold;
    scrollbar-arrow-color:blue;
    scrollbar-base-color:;
    scrollbar-darkshadow-color:blue;
    scrollbar-face-color:;
    scrollbar-highlight-color:;
    scrollbar-shadow-color:
}

Chrome 和 Safari (WebKit)

同样,WebKit 现在也有自己的版本:

火狐(壁虎)

从版本 64 开始,Firefox 通过属性 scrollbar-color(部分是 W3C draft)和 scrollbar-width (W3C draft) 支持滚动条样式。可以在 this answer 中找到有关实现的一些有用信息。 .

跨浏览器滚动条样式

JavaScript 库和插件可以提供跨浏览器的解决方案。有很多选择。

列表可以继续。您最好的选择是搜索、研究和测试可用的解决方案。我相信您将能够找到适合您需求的东西。

防止非法滚动条样式

以防万一您想阻止没有正确前缀“-vendor”的滚动条样式,this article over at W3C provides some basic instructions .基本上,您需要将以下 CSS 添加到与您的浏览器关联的用户样式表中。这些定义将覆盖您访问的任何页面上的无效滚动条样式。

body, html {
  scrollbar-face-color: ThreeDFace !important;
  scrollbar-shadow-color: ThreeDDarkShadow !important;
  scrollbar-highlight-color: ThreeDHighlight !important;
  scrollbar-3dlight-color: ThreeDLightShadow !important;
  scrollbar-darkshadow-color: ThreeDDarkShadow !important;
  scrollbar-track-color: Scrollbar !important;
  scrollbar-arrow-color: ButtonText !important;
}

重复或类似的问题/以上未链接来源

注意:此答案包含来自各种来源的信息。如果使用了来源,那么它也会在此答案中链接。

关于div中的CSS自定义滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9251354/

相关文章:

javascript - Jquery如果$this属性选择器包含一个词

javascript - 如何在 href 中间添加变量?

html - 为什么 body 宽度不扩展到其子元素的宽度

css - 如何在 CSS 中创建这样的布局?

html - CSS html 图像不堆叠

jquery - 当使用 jquery 添加/删除类时,第 Nth-Child/Nth-Of-Type 不能使用 CSS 类?

javascript - 无法在 js 上填充 ddl

WPF DataGrid 不会在拖动时滚动

ios - jquery 移动数据角色页面或内容中的奇怪滚动

javascript - jQuery 不会影响 ul 的第二个子级