隐藏浏览器上的垂直滚动条,但使其仍能正常工作

Hide vertical scrollbar on browsers but making it still working

提问人:Matt 提问时间:9/24/2021 更新时间:9/27/2021 访问量:60

问:

我必须隐藏元素的垂直滚动条,但我必须使“滚动”继续工作,例如使用鼠标滚轮。

关注了这篇文章

.container {
    overflow-y: scroll;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none;  /* Internet Explorer 10+ */
}
.container::-webkit-scrollbar { /* WebKit */
    width: 0;
    height: 0;
}

使用该代码,我能够隐藏两个滚动条,但我无法保持水平滚动条可见。 有谁知道该怎么做。?

CSS 谷歌 互联网浏览器 Firefox 滚动条

评论


答:

1赞 Yu Zhou 9/27/2021 #1

您想显示水平滚动条并隐藏垂直滚动条,但使两者都正常工作吗?如果是这样,您可以参考下面的代码:

.content {
  width: 400px;
  height: 200px;
}

.container {
  overflow-x: auto;
  overflow-y: auto;
  height: 100px;
  width: 200px;
  scrollbar-width: none;
  /* Firefox */
  -ms-overflow-style: none;
  /* Internet Explorer 10+ */
}

.container::-webkit-scrollbar {
  height: 8px;
  width: 0px;
  border: 1px solid #fff;
}

 ::-webkit-scrollbar-track {
  border-radius: 0;
  background: #eeeeee;
}

 ::-webkit-scrollbar-thumb {
  border-radius: 0;
  background: #b0b0b0;
}
<div class="container">
  <div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>

此代码适用于 webkit 浏览器。但是对于 FireFox 和 IE,我们似乎只能隐藏或显示两个滚动条,因为它们无法自定义 scrollbar-track 和 scrollbar-thumb。

评论

0赞 Matt 9/29/2021
谢谢男人:)