手机 css 滚动条 隐藏 水平移动
由于只需要兼容移动浏览器(Chrome 和 Safari),于是想到了自定义滚动条的伪对象选择器
::-webkit-scrollbar。
关于这个选择器的介绍可以参考:
Styling Scrollbars
Custom Scrollbars in WebKit
移动端:
直接在出现滚动条的容器添加以下属性 /* for Chrome */ .inner-container::-webkit-scrollbar { display: none; }
pc端:
如果要兼容 PC 其他浏览器(IE、Firefox 等),国外一位才人 John Kurlak 也研究出了一种办法。在容器外面再嵌套一层 overflow:hidden 内部内容再限制尺寸和外部嵌套层一样,就变相隐藏了。
html:
<div class="outer-container"> <div class="inner-container"> <div class="content"> ...... </div> </div> </div>
css:
.outer-container,.content { width: 200px; height: 200px; } .outer-container { position: relative; overflow: hidden; } .inner-container { position: absolute; left: 0; overflow-x: hidden; overflow-y: scroll; } /* for Chrome */ .inner-container::-webkit-scrollbar { display: none; }
https://blog.niceue.com/front-end-development/hide-scrollbar-but-still-scrollable-using-css.html