【问题标题】:Hide vertical scrollbar, keep horizontal and still able to scroll隐藏垂直滚动条,保持水平并且仍然可以滚动
【发布时间】:2020-07-27 08:04:00
【问题描述】:

我正在尝试以一种可以垂直和水平滚动的 div 的方式编写 HTML/CSS,但只有水平滚动条可见。因此,垂直滚动是使用鼠标滚轮完成的,水平滚动是使用底部的滚动条(以及其他常用控件,如键盘等)完成的。通常建议的带有 -webkit-scrollbar display none 的解决方案在这里不适用,因为它只能隐藏两个滚动条,因此是新问题。

所以,基本上,我需要这样的行为,除了水平滚动条应该仍然存在:

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

.container {
  overflow-x: auto;
  overflow-y: auto;
  height: 100px;
  width: 200px;
}

.container::-webkit-scrollbar {
  display: none;
}
<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>

【问题讨论】:

  • 寻求代码帮助的问题必须包含重现它所需的最短代码在问题本身中最好在Stack Snippet 中。见How to create a Minimal, Reproducible Example
  • 抱歉,没有看到添加代码的必要,因为这个问题非常简单(甚至在我添加代码之前就得到了答案)。在提出问题之前,我添加了我的方法的最小示例,希望这就足够了。

标签: html css


【解决方案1】:

你可以使用-webkit-scrollbar,但你需要使用widthheight而不是display: none

div {
  width: 100px;
  height: 100px;
  font-size: 48px;
  
  overflow: auto;
}

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

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

::-webkit-scrollbar-thumb {
  border-radius: 0;
  background: #b0b0b0;
}
<div>
Lorem ipsum dolor sit amet.
</div>

Shift scroll to move horizontally

width 只会影响垂直滚动,而height 只会影响水平滚动。如果设置height: 0px,它会隐藏水平滚动条,但保持垂直滚动条。

【讨论】:

  • 那么 IE 或 Firefox 呢?如何在这些浏览器中执行此操作?
  • @desert,我不知道。看看this。您可能必须有一个滚动条设置为none 的包装器。 IE 的 EOL 为明年 6 月。我相信你可以想出办法让它工作,但在大多数情况下,这可能比它的价值更麻烦。
  • 我进行了两次潜水,一次又一次。我向父级添加了溢出-y:滚动和滚动条宽度:无,向子级添加了溢出-x:自动。可能这不是最好的方法,但它确实有效。
猜你喜欢
  • 2022-01-01
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
  • 2013-05-16
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多