HTML & CSS – Styling Scrollbar
前言
Scrollbar 能 styling 的东西不多 (尤其是 IOS 基本上只能 display:none 而已),但有时候我们不得不 styling。
这里记入我自己在项目中修改过的 scrollbar 经历。
参考
例子
Gmail Scrollbar
Gmail 的 scrollbar 就改过 style (为了美观)。
size, color, border-radius 这几个地方都改过。
Ads Scrollbar

像这种可以 sticky vertical 的 scrollbar 是模拟出来的,但是它需要把 native scrollbar hide 起来,而且不可以使用 overflow hidden,因为虽然 scrollbar 要 hide 但是 touch move scroll 功能是要保留的,
所以只能通过 styling 的方式去 hide。
Styling Webkit Scrollbar (not Firefox, not IOS)
Chrome, Edge 会比较可以 styling scrollbar。FireFox,IOS 几乎不支持。
::webkit-scrollbar
.container {
&::-webkit-scrollbar {
height: 4px; // for vertical scrollbar
width: 4px; // for horizontal scrollbar
background-color: red; // for both
}
}
修改 size, color。
它是透过伪元素 (pseudo-elements) 选择器来实现的,这也意味着我们无法透过 JS 来 styling scrollbar。
效果

红色的部分就是 scrollbar area。
也可以搭配 hover 才换颜色
.container {
&::-webkit-scrollbar {
background: transparent;
}
&::-webkit-scrollbar:hover {
background: red;
}
}
效果

如果想 hide scrollbar 可以设置 width 或 height = 0
::-webkit-scrollbar-thumb
.container {
&::-webkit-scrollbar-thumb {
border-radius: 999px;
}
&::-webkit-scrollbar-thumb:vertical {
background-color: blue;
}
&::-webkit-scrollbar-thumb:horizontal {
background-color: green;
}
}
thumb 指的是我们拉动的区域,修改了 color 和 border-radius
提醒:那个 vertical 和 horizontal 伪类 (pseudo-classes) 选择器在 ::webkit-scrollbar 是不支持的哦。
效果

::-webkit-scrollbar-button
.container {
&::-webkit-scrollbar-button:horizontal {
background-color: purple;
}
}
效果

Styling Firefox Scrollbar
Firefox 非常局限
.container {
scrollbar-color: red orange; // first for thumb, second for track
scrollbar-width: thin; // 只能选 auto, thin, none
}
只能改 size 和 color,而且 size 只能选 auto, thin, none。
效果

这 2 个属性,Chrome 和 Edge 也支持,但 Safari 不支持。
Styling IOS Scrollbar
IOS 只支持下面这句
.container {
&::-webkit-scrollbar {
display: none;
}
}
只能 display: none 其它都不支持。
总结
我个人的 Best Practice 是,只有 Chrome 和 Edge 可以 styling for 美观以外,Firefox 和 Safari 只能 styling for hide scrollbar。
HTML & CSS – Styling Scrollbar的更多相关文章
- 用CSS调整scrollbar(滚动条)的配色
可以通过调整CSS的方式,来给滚动条换色. 代码如下: .uicss-cn{ height:580px;overflow-y: scroll; scrollbar-face-color:#EAEAEA ...
- CSS:scrollbar的属性及样式分类
overflow 内容溢出时的设置(设定被设定对象是否显示滚动条) overflow-x 水平方向内容溢出时的设置 overflow-y 垂直方向 ...
- 【转】Styling And Animating SVGs With CSS
原文转自:http://www.smashingmagazine.com/2014/11/03/styling-and-animating-svgs-with-css/?utm_source=CSS- ...
- 自定义文件上传的按钮的样式css+js
核心就是一段css遮住了原生的input框,然后用js将文件的值传入到另一个指定的input框中 原文链接 http://geniuscarrier.com/how-to-style-a-html-f ...
- HTML&CSS学习笔记
<table> <thead> <tr> // table row <th></th> // table head & ...
- [codecademy]fonts in css
Great job! You learned how to style an important aspect of the user experience: fonts! Let's review ...
- 纯div+css制作的弹出菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CSS 浏览器兼容
1. 兼容 IF <!--[if lte IE 7]> <style type="text/css"> .menu { position:relative ...
- [Angular] How to styling ng-content
Let's say you are builing a reuseable component. The style structure like this: div > input If yo ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
随机推荐
- webgl未使用独立显卡报告2
楔子 在上一篇文章 <# [https://juejin.cn/post/7074771064286347301] webgl未使用独立显卡报告> 发表后,有读者在公众号给我发了一段评论, ...
- PowerShell pnpm 报错
Vue3> pnpm run dev pnpm : 无法加载文件 D:\program files\nodejs\node_global\pnpm.ps1.未对文件 D:\program fil ...
- AT_abc218_d 题解
洛谷链接&Atcoder 本篇题解为此题较简单做法及较少码量,并且码风优良,请放心阅读. 题目简述 给定一个平面内的 \(N\) 个点的坐标,求这 \(N\) 个点中选 \(4\) 个点可构成 ...
- windows下mysql服务局域网访问配置
在局域网中访问本机(Windows)的MySQL服务器,需要确保MySQL服务器配置为允许远程访问,并且防火墙设置允许外部连接.以下是详细的步骤: 1. 修改MySQL配置文件允许远程访问 找到并编辑 ...
- nacos一个奇怪的问题:找不到正确的应用名-环境.后缀名配置文件
spring: profiles: active: dev application: name: system-api cloud: nacos: server-addr: 192.168.101.6 ...
- python变量 方法 属性
python变量 方法 属性 所有成员中,只有普通变量/字段/属性的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通变量/字段/属性.而其他的成员,则都是保存在类中,即:无论对象的多 ...
- Python和RPA网页自动化-让select标签下拉框选择指定文本的方法
以影刀商城(https://shop.yingdao.com/webOperations/index)为例: 该下拉框有<select>标签.分别使用Python和RPA网页自动化让下拉框 ...
- 记一次eclipse导入的JavaEE项目无法连接数据库的排查
1.问题描述 Eclipse导入了一个JavaEE项目 在虚拟机环境中新建了一个数据库 数据库可以使用本地客户端工具正常连接 导入的JavaEE项目修改了数据源配置后无法启动 相同的数据源配置通过在I ...
- 【ActiveJdbc】05
一.事务 通常在 Java ORM 中有一个显式连接或管理器对象(JPA 中的 EntityManager,Hibernate 中的 SessionManager 等). ActiveJDBC 中没有 ...
- 【JSON】JavaScript Object Notation JS对象表示规则
什么是 JSON? JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. JSON采用完全独立于语言的文本格式 ...
