4 回答

TA貢獻1817條經(jīng)驗 獲得超6個贊
這適用于簡單的CSS屬性:
.container { -ms-overflow-style: none; // IE 10+ scrollbar-width: none; // Firefox}.container::-webkit-scrollbar { display: none; // Safari and Chrome}
更新:scrollbar-width
為Firefox 添加了新屬性。
對于舊版本的Firefox,請使用: overflow: -moz-scrollbars-none;

TA貢獻1874條經(jīng)驗 獲得超12個贊
更新: Firefox現(xiàn)在支持使用CSS隱藏滾動條,因此現(xiàn)在涵蓋了所有主流瀏覽器(Chrome,F(xiàn)irefox,IE,Safari等)。
只需將以下CSS應用于要從中刪除滾動條的元素:
.container { overflow-y: scroll; scrollbar-width: none; /* Firefox */ -ms-overflow-style: none; /* IE 10+ */}.container::-webkit-scrollbar { /* WebKit */ width: 0; height: 0;}
這是我目前所知道的最少hacky跨瀏覽器解決方案。看看演示。
原始答案:
這是另一種尚未提及的方式。它非常簡單,只涉及兩個div和CSS。不需要JavaScript或?qū)S蠧SS,它適用于所有瀏覽器。它不需要明確地設置容器的寬度,從而使其流動。
此方法使用負邊距將滾動條移出父級,然后使用相同數(shù)量的填充將內(nèi)容推回到其原始位置。該技術適用于垂直,水平和雙向滾動。
演示:
垂直版本的示例代碼:
HTML:
<div class="parent"> <div class="child"> Your content. </div></div>
CSS:
.parent{ width: 400px; height: 200px; border: 1px solid #aaa; overflow: hidden;}.child{ height: 100%; margin-right: -50px; /* maximum width of scrollbar */ padding-right: 50px; /* maximum width of scrollbar */ overflow-y: scroll;}

TA貢獻1911條經(jīng)驗 獲得超7個贊
只是一個工作正常的測試。
#parent{ height: 100%; width: 100%; overflow: hidden;}#child{ width: 100%; height: 100%; overflow-y: scroll; padding-right: 17px; /* Increase/decrease this value for cross-browser compatibility */ box-sizing: content-box; /* So the width will be 100% + 17px */}
JavaScript的:
由于滾動條寬度在不同瀏覽器中有所不同,因此最好使用JavaScript處理它。如果這樣做Element.offsetWidth - Element.clientWidth
,將顯示確切的滾動條寬度。
要么
使用Position: absolute
,
#parent{ height: 100%; width: 100%; overflow: hidden; position: relative;}#child{ position: absolute; top: 0; bottom: 0; left: 0; right: -17px; /* Increase/Decrease this value for cross-browser compatibility */ overflow-y: scroll;}
信息:
根據(jù)這個答案,我創(chuàng)建了一個簡單的滾動插件。我希望這會對某人有所幫助。

TA貢獻2080條經(jīng)驗 獲得超4個贊
在WebKit中很容易,有可選的樣式:
html { overflow: scroll; overflow-x: hidden;}::-webkit-scrollbar { width: 0px; /* Remove scrollbar space */ background: transparent; /* Optional: just make scrollbar invisible */}/* Optional: show position indicator in red */: :-webkit-scrollbar-thumb { background: #FF0000;}
添加回答
舉報