CSS可以用来隐藏滚动条吗?如何实现这个效果?
除了彼得的回答:
#element::-webkit-scrollbar {
display: none;
}
这对于Internet Explorer 10也将起同样的作用:
#element {
-ms-overflow-style: none;
}
跨浏览器隐藏滚动条的方法。
在Edge、Chrome、Firefox和Safari上进行了测试。
隐藏滚动条,同时仍然可以使用鼠标滚轮滚动!
/* Make parent invisible */
#parent {
visibility: hidden;
overflow: scroll;
}
/* Safari and Chrome specific style. Don't need to make parent invisible, because we can style WebKit scrollbars */
#parent:not(*:root) {
visibility: visible;
}
/* Make Safari and Chrome scrollbar invisible */
#parent::-webkit-scrollbar {
visibility: hidden;
}
/* Make the child visible */
#child {
visibility: visible;
}
overflow: hidden
(或 overflow-y)设置在 body
元素上不能隐藏滚动条。
我必须使用 html
元素。
我最大的担忧是所有浏览器版本是否都会设置滚动条以显示溢出的内容。我已经在当前浏览器中进行了测试,但没有在旧版浏览器中测试过。
请原谅我的SASS ;P
%size {
// set width and height
}
.outer {
// mask scrollbars of child
overflow: hidden;
// set mask size
@extend %size;
// has absolutely positioned child
position: relative;
}
.middle {
// always have scrollbars.
// don't use auto, it leaves vertical scrollbar showing
overflow: scroll;
// without absolute, the vertical scrollbar shows
position: absolute;
}
// prevent text selection from revealing scrollbar, which it only does on
// some webkit based browsers.
.middle::-webkit-scrollbar {
display: none;
}
.content {
// push scrollbars behind mask
@extend %size;
}
OS X 版本为 10.6.8。Windows 版本为 Windows 7。
%size{}
代替@mixin{}
,然后在css选择器中调用@extend %size;
。当您拉取变量以返回结果时,通常会使用mixin。占位符(又名@extend)适用于像这样的简单重复代码 - 不需要“函数”的情况。 - Mike Barwick<style>
::-webkit-scrollbar {
display: none;
}
#element::-webkit-scrollbar {
display: none;
}
</style>
这是一个段落。
<div class="container">
<div class="content">
</div>
</div>
.content {
position: absolute;
right: -100px;
overflow-y: auto;
overflow-x: hidden;
height: 75%; /* This can be any value of your choice */
}
.container {
overflow-x: hidden;
max-height: 100%;
max-width: 100%;
}
我编写了一个带有一些选项的WebKit版本,如自动隐藏,小版本,只滚动垂直方向或水平方向:
._scrollable{
@size: 15px;
@little_version_ratio: 2;
@scrollbar-bg-color: rgba(0,0,0,0.15);
@scrollbar-handler-color: rgba(0,0,0,0.15);
@scrollbar-handler-color-hover: rgba(0,0,0,0.3);
@scrollbar-coner-color: rgba(0,0,0,0);
overflow-y: scroll;
overflow-x: scroll;
-webkit-overflow-scrolling: touch;
width: 100%;
height: 100%;
&::-webkit-scrollbar {
background: none;
width: @size;
height: @size;
}
&::-webkit-scrollbar-track {
background-color:@scrollbar-bg-color;
border-radius: @size;
}
&::-webkit-scrollbar-thumb {
border-radius: @size;
background-color:@scrollbar-handler-color;
&:hover{
background-color:@scrollbar-handler-color-hover;
}
}
&::-webkit-scrollbar-corner {
background-color: @scrollbar-coner-color;
}
&.little{
&::-webkit-scrollbar {
background: none;
width: @size / @little_version_ratio;
height: @size / @little_version_ratio;
}
&::-webkit-scrollbar-track {
border-radius: @size / @little_version_ratio;
}
&::-webkit-scrollbar-thumb {
border-radius: @size / @little_version_ratio;
}
}
&.autoHideScrollbar{
overflow-x: hidden;
overflow-y: hidden;
&:hover{
overflow-y: scroll;
overflow-x: scroll;
-webkit-overflow-scrolling: touch;
&.only-y{
overflow-y: scroll !important;
overflow-x: hidden !important;
}
&.only-x{
overflow-x: scroll !important;
overflow-y: hidden !important;
}
}
}
&.only-y:not(.autoHideScrollbar){
overflow-y: scroll !important;
overflow-x: hidden !important;
}
&.only-x:not(.autoHideScrollbar){
overflow-x: scroll !important;
overflow-y: hidden !important;
}
}
即使使用overflow:hidden;
,我的答案也会滚动,使用jQuery:
例如,使用鼠标滚轮水平滚动:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type='text/javascript' src='/js/jquery.mousewheel.min.js'></script>
<script type="text/javascript">
$(function() {
$("YourSelector").mousewheel(function(event, delta) {
this.scrollLeft -= (delta * 30);
event.preventDefault();
});
});
</script>
overflow
来控制它,但是它的浏览器支持有限。一些来源表示支持Internet Explorer 5(及更高版本),Firefox 1.5(及更高版本)和Safari 3(及更高版本)- 这可能已经足够满足您的需求。
Scrolling, Scrolling, Scrolling有一个很好的讨论。