CSS可以用来隐藏滚动条吗?你会怎么做?
当前回答
设置溢出:隐藏;在body标签上,如下所示:
<style type="text/css">
body {
overflow: hidden;
}
</style>
上面的代码“隐藏”了水平和垂直滚动条。
如果要仅隐藏垂直滚动条,请使用overflow-y:
<style type="text/css">
body {
overflow-y: hidden;
}
</style>
如果您只想隐藏水平滚动条,请使用overflow-x:
<style type="text/css">
body {
overflow-x: hidden;
}
</style>
如果需要,内容将被剪裁以适合填充框。不提供滚动条,也不允许用户滚动(例如通过拖动或使用滚轮)。内容可以通过编程方式滚动(例如,通过设置属性值,如offsetLeft),因此元素仍然是滚动容器。(来源)
其他回答
即使溢出,我的答案也会滚动:隐藏;,使用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>
我的HTML是这样的:
<div class="container">
<div class="content">
</div>
</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%;
}
您可以使用隐藏溢出的包装器div,并将内部div设置为auto来实现这一点。
要删除内部div的滚动条,可以通过对内部div应用负边距将其从外部div的视口中拉出。然后对内部div使用相等的填充,以使内容保持在视图中。
JSFiddle公司
###HTML格式
<div class="hide-scroll">
<div class="viewport">
...
</div>
</div>
###CSS格式
.hide-scroll {
overflow: hidden;
}
.viewport {
overflow: auto;
/* Make sure the inner div is not larger than the container
* so that we have room to scroll.
*/
max-height: 100%;
/* Pick an arbitrary margin/padding that should be bigger
* than the max scrollbar width across the devices that
* you are supporting.
* padding = -margin
*/
margin-right: -100px;
padding-right: 100px;
}
设置溢出:隐藏;在body标签上,如下所示:
<style type="text/css">
body {
overflow: hidden;
}
</style>
上面的代码“隐藏”了水平和垂直滚动条。
如果要仅隐藏垂直滚动条,请使用overflow-y:
<style type="text/css">
body {
overflow-y: hidden;
}
</style>
如果您只想隐藏水平滚动条,请使用overflow-x:
<style type="text/css">
body {
overflow-x: hidden;
}
</style>
如果需要,内容将被剪裁以适合填充框。不提供滚动条,也不允许用户滚动(例如通过拖动或使用滚轮)。内容可以通过编程方式滚动(例如,通过设置属性值,如offsetLeft),因此元素仍然是滚动容器。(来源)
要禁用垂直滚动条,只需添加overflow-y:hidden;。
了解更多信息:溢出。