移動端頁面爲了更接近原生的體驗,是否能夠隱藏滾動條,同時又保證頁面能夠滾動?css
使用 overflow:hidden
隱藏滾動條,但存在的問題是:頁面或元素失去了滾動的特性。
因爲只須要兼容移動瀏覽器(Chrome 和 Safari),因而想到了自定義滾動條的僞對象選擇器::-webkit-scrollbar
。html
關於這個選擇器的介紹能夠參考:
Styling Scrollbars
Custom Scrollbars in WebKitweb
應用以下 CSS 能夠隱藏滾動條:chrome
.element::-webkit-scrollbar {display:none}
若是要兼容 PC 其餘瀏覽器(IE、Firefox 等),國外一位才人 John Kurlak 也研究出了一種辦法。在容器外面再嵌套一層 overflow:hidden
內部內容再限制尺寸和外部嵌套層同樣,就變相隱藏了。瀏覽器
<div class="outer-container"> <div class="inner-container"> <div class="content"> ...... </div> </div> </div> .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; }
參考:Hiding Vertical Scrollbars with Pure CSS in Chrome, IE (6+), Firefox, Opera, and Safariide
https://blog.niceue.com/front-end-development/hide-scrollbar-but-still-scrollable-using-css.htmlpost