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),因此元素仍然是滚动容器。(来源)

其他回答

隐藏滚动条的跨浏览器方法。

它在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;
}

除了彼得的回答:

#element::-webkit-scrollbar {
    display: none;
}

这对Internet Explorer 10的作用相同:

 #element {
      -ms-overflow-style: none;
 }

CSS可以用来隐藏滚动条吗?你会怎么做?

如果要从浏览器视口中删除垂直(和水平)滚动条,请添加:

style="position: fixed;"

到<body>元素。


Java脚本:

document.body.style.position = 'fixed';

CSS:

body {
  position: fixed;
}

要禁用垂直滚动条,只需添加overflow-y:hidden;。

了解更多信息:溢出。

您可以使用隐藏溢出的包装器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;
}