前言

Scrollbar 能 styling 的东西不多 (尤其是 IOS 基本上只能 display:none 而已),但有时候我们不得不 styling。

这里记入我自己在项目中修改过的 scrollbar 经历。

参考

can i use webkit-scrollbar

MDN – ::-webkit-scrollbar

MDN – scrollbar-width

MDN – scrollbar-color

例子

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的更多相关文章

  1. 用CSS调整scrollbar(滚动条)的配色

    可以通过调整CSS的方式,来给滚动条换色. 代码如下: .uicss-cn{ height:580px;overflow-y: scroll; scrollbar-face-color:#EAEAEA ...

  2. CSS:scrollbar的属性及样式分类

    overflow            内容溢出时的设置(设定被设定对象是否显示滚动条) overflow-x         水平方向内容溢出时的设置 overflow-y         垂直方向 ...

  3. 【转】Styling And Animating SVGs With CSS

    原文转自:http://www.smashingmagazine.com/2014/11/03/styling-and-animating-svgs-with-css/?utm_source=CSS- ...

  4. 自定义文件上传的按钮的样式css+js

    核心就是一段css遮住了原生的input框,然后用js将文件的值传入到另一个指定的input框中 原文链接 http://geniuscarrier.com/how-to-style-a-html-f ...

  5. HTML&CSS学习笔记

    <table> <thead> <tr>            // table row <th></th> // table head & ...

  6. [codecademy]fonts in css

    Great job! You learned how to style an important aspect of the user experience: fonts! Let's review ...

  7. 纯div+css制作的弹出菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. CSS 浏览器兼容

    1.  兼容 IF <!--[if lte IE 7]> <style type="text/css"> .menu { position:relative ...

  9. [Angular] How to styling ng-content

    Let's say you are builing a reuseable component. The style structure like this: div > input If yo ...

  10. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

随机推荐

  1. 使用FModel提取游戏资产

    目录 前言 FModel简介 FModel安装 FModel使用 初次使用 资产预览 资产导出 附录 dumper Dumper-7生成usmap文件 向游戏中注入dll 前言 这篇文章仅记录我作为初 ...

  2. Django REST framework的10个常见组件

    Django REST framework的10个常见组件: 权限组件 认证组件 访问频率限制组件 序列化组件 路由组件 视图组件 分页组件 解析器组件 渲染组件 版本组件

  3. oeasy教您玩转vim - 28 - 水平移动

    ​ 水平移动 回忆上节课内容 根据扩展名我们可以设置某些特定类型文件的配置 相关文件类型的设置放在相应的文件夹里 文件类型缩进文件夹 /usr/share/vim/vim81/indent/ 文件类型 ...

  4. npm私服 verdaccio 搭建

    1.什么是npm 私服 我们前端(web,nodejs)平常使用的各种包,什么vue,react,react-router, zustand等,都会从 https://registry.npmjs.o ...

  5. 涨见识了!脱离vue项目竟然也可以使用响应式API

    前言 vue3的响应式API大家应该都特别熟悉,比如ref.watch.watchEffect等.平时大家都是在vue-cli或者vite创建的vue项目里面使用的这些响应式API,今天欧阳给大家带来 ...

  6. 使用nvm管理node

    安装包在阿里云盘 安装时,记录安装位置 安装后,在安装目录的setting添加镜像地址 node_mirror:npm.taobao.org/mirrors/node/ npm_mirror:npm. ...

  7. 【Vue2】Direct 指令

    1.内容渲染指令 1.插值表达式 2.V - TEXT 3.V - HTML <!DOCTYPE html> <html lang="en"> <he ...

  8. 【ElasticSearch】02 查询操作

    准备样本: Elasticsearch 提供了基于 JSON 提供完整的查询 DSL 来定义查询 查询条件还适用于删除操作   创建索引: # PUT http://127.0.0.1:9200/st ...

  9. 【Git】GithubDesktop 忽略文件无法忽略BUG

    问题描述: 从仓库拉取的[.gitignore]忽略配置文件,在项目跑起来之后会生成诸多.idea文件,target打包文件 一开始没有忽略,但是发现使用GD配置之后忽略无效: 解决办法: 做一次随便 ...

  10. 跨平台字幕制作软件ARCTIME

      官网:http://arctime.cn/