CSS3 - 如何“恢复”CSS3 ::-webkit-scrollbar 属性设置为默认滚动条
您好,我正在使用下一个 css 代码来设置 Safari 和 Chrome 中滚动条的样式。效果非常好,但我面临下一个问题,当我在 iPad 上查看该网站时,我希望恢复默认值。我正在使用 @media css 来实现此目的,但是我不知道如何恢复默认值。
@media screen and (min-width: 768px) and (max-width: 1024px) { }
/*Scroll bar nav*/
::-webkit-scrollbar {
width: 12px;
}
/* Track */
::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
-webkit-border-radius: 10px;
border-radius: 10px;
background:#FFF;
}
/* Handle */
::-webkit-scrollbar-thumb {
-webkit-border-radius: 10px;
border-radius: 10px;
background: rgba(204,204,204,0.8);
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);
}
::-webkit-scrollbar-thumb:window-inactive {
background: rgba(204,204,204,0.4);
}
Hi I'm using the next css code to style scroll bars in Safari and Chrome. And works really great but I´m facing the next issue, I would like te restore the default value, when I view the site on my ipad. I'm using @media css for achived this but, I don't know how to restore the defaults values.
@media screen and (min-width: 768px) and (max-width: 1024px) { }
/*Scroll bar nav*/
::-webkit-scrollbar {
width: 12px;
}
/* Track */
::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
-webkit-border-radius: 10px;
border-radius: 10px;
background:#FFF;
}
/* Handle */
::-webkit-scrollbar-thumb {
-webkit-border-radius: 10px;
border-radius: 10px;
background: rgba(204,204,204,0.8);
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);
}
::-webkit-scrollbar-thumb:window-inactive {
background: rgba(204,204,204,0.4);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新 2022
我大约 10 年前回答过这个问题,似乎在 2021 年之后这个解决方案停止工作,阅读 @Volomike 的解决方案,它可能会让你到达你想要的地方。
我刚刚意识到你可以在 auto 中设置所有属性;并会成功。这是一个自我回答,但我想有一天有人会提出同样的问题。
我不知道是否存在其他方法。
- 更新 -
看起来您还可以使用
initial
和unset
值//恢复所有值
或应用于特定值
{width : unset}
||{width : initial}
注意:
使用 unset 在 IE11 上不起作用UPDATE 2022
I answered this almost 10 years ago and seems like after 2021 this solution stop working, read the solution from @Volomike, it might get you where you want to.
I just realized you can set all the properties in auto; and will do the trick. This is a self answer but I guess someday someone can have the same question.
I don't know if exist another method.
-- UPDATE --
Look like you can also use the
initial
andunset
value//reverting all the values
or apply to an specific one
{width : unset}
||{width : initial}
NOTE:
Using unset will not work on IE11使用
初始
值或<您想要的属性的 href="https://developer.mozilla.org/en-US/docs/Web/CSS/unset" rel="noreferrer">取消设置
值恢复(取决于您具体如何想要恢复它们)。这两个值都可以应用于所有 CSS 属性。
示例
或者
如果您想恢复规则的所有属性,那么您应该使用
all
关键字示例
注意: IE 目前还不支持其中任何一个。
每个浏览器的支持级别不同(有关详细信息,请参阅链接文档)
Use the
initial
value orunset
value for the properties you want to revert (depending on how exactly you want to revert them).Both these values can be applied to all CSS properties.
example
or
If you want to revert all properties of a rule then you should use the
all
keywordexample
Notice: No IE support for any of these as of yet.
Varying levels of support for each browser (see the linked docs for details)
我遇到了麻烦。我不知道到底是什么触发了最新的 Chrome 将桌面浏览器上的滚动条切换到覆盖模式,但这让我感到不安,因为它使页面滚动条对于没有经验的用户来说看起来很糟糕。所选答案似乎不适用于我的 Lubuntu Linux 20.04.1 版本 100.0.4896.127 上的 Chrome 版本。因此,在几个小时的时间里,我煞费苦心地重新创建了 Chrome 系统默认设置,并且在浅色和深色模式下也能工作。请注意,在此示例中,我仅设置 BODY 元素的垂直滚动条的样式,但您可以轻松适应水平滚动条,而不仅仅是将其应用于 BODY 元素。
额外说明:
filter:brightness(x);
级别或opacity:x
级别在控件上创建不同级别的明暗度 - - 但奇怪的是,这些在这些滚动条上不起作用。相反,我发现在大范围内设置一个内嵌box-shadow
,而没有其他任何东西,使用 rgba 颜色,效果很好。width:initial
加上height:auto
就足以设置滚动条控制部分的宽度。fill
更改为ButtonText
,以便使其在浏览器深色和浅色模式下工作。image-rendering:pixelated
非常有用,因为否则非常小的上下三角形 SVG 图标会被过度抗锯齿,以至于看起来太不透明。这使得 SVG 图标能够在浅色模式下保持其暗度,在暗色模式下保持亮度。我向 Chromium 浏览器团队提交了这个问题,看看他们是否可以为我们提供一些可靠的 CSS 来关闭给定元素上的覆盖滚动条(如果我们需要的话)。
https://bugs.chromium.org/p/chromium/issues/ detail?id=1333947
编辑: 最近,我发现网络服务器缓存与 Google Chrome/Chromium/Edge Chromium 浏览器有关至少在 2022 年 6 月 17 日左右版本 100+ 左右。当我打开 Web 服务器缓存时,网站上返回上一页的任何子页面(作为常规相对链接,而不是
history.back();
调用)将始终显示覆盖滚动条而不是固定滚动条。但是,如果我强制使用 .htaccess 规则(该规则是网站缓存破坏者)的页面,那么问题就会消失,我会看到像平常一样的滚动条。I had trouble with this. I don't know what exactly triggers the latest Chrome to switch scrollbars on a desktop browser to overlay mode, but it was unnerving to me because it makes the page scroller look broken to an inexperienced user. The selected answer didn't seem to work in my version of Chrome on Lubuntu Linux 20.04.1, version 100.0.4896.127. So, over several hours, I painstakingly recreated the settings to Chrome's system defaults and yet something that works in both light and dark mode too. Note I'm only styling the BODY element's vertical scroller in this example, but you can adapt easily for horizontal scrollers as well as not just apply it to the BODY element.
Extra Notes:
filter:brightness(x);
level or anopacity:x
level to create your varying levels of light and dark on the control -- but those oddly don't work on these scrollbars. Instead, I found that that an insetbox-shadow
set on wide spread, and nothing else, with rgba colors, worked well.width:initial
, plusheight:auto
, on the button alone was enough to set the width of the scrollbar control parts.fill
on those toButtonText
in order to make it work in browser dark and light modes.rgba(128,128,128,x)
in some cases because starting from white or black with opacity created odd issues when switching light and dark modes, and so I chose the middle value with opacity to get around that.image-rendering:pixelated
was very useful because otherwise the very tiny up and down triangle SVG icon would be antialiased so much that it would look too opaque. This allowed the SVG icon to maintain its darkness on light mode, and brightness on dark mode.I filed this issue with the Chromium browser team to see if they can give us some reliable CSS to turn off overlay scrollbars on a given element, should we need that.
https://bugs.chromium.org/p/chromium/issues/detail?id=1333947
EDIT: Recently, I found that web server cache has something to do with the Google Chrome/Chromium/Edge Chromium browsers from somewhere around version 100+ at least here around 6/17/2022. When I turn on web server cache, any sub-page on a website going back to a previous page (as a regular relative link, not as a
history.back();
call), will consistently show an overlay scrollbar instead of a fixed scrollbar. But if I force the page with an .htaccess rule that is a website cache buster, then the problem goes away and I see a scrollbar like normal.正如评论者 vsync 提到的,一旦你隐藏了滚动条,如果你添加一个新的 css 规则来显示滚动条,它就不会按预期工作。
可以用JS解决
As commenter vsync mentioned, once you've hid the scrollbar, if you add a new css rule to show the scrollbar, it doesn't work as expected.
Can be solved with JS