我正在努力想办法让一个可滚动的div在悬停时只显示滚动条.

例如谷歌图片搜索,在下面的图片中,你可以看到左侧边栏在鼠标悬停在上面之前是无法滚动的.

这在CSS中是可能的还是需要Javascript?如果可能的话,可以举一个简单的例子来说明如何完成这样的任务?

示例

推荐答案

div {
  height: 100px;
  width: 50%;
  margin: 0 auto;
  overflow: hidden;
}

div:hover {
  overflow-y: scroll;
}
<div>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </p>
</div>

这样的东西行得通吗?

Css相关问答推荐

如何在CSS中使用nextJs来指定基色?

使用间距时奇怪的MUI网格行为

关闭Superset中的Deck.gl工具提示

如何缩小 Select 元素的背景图像?

媒体查询在生产中没有响应:React 18 应用程序

动态覆盖 Vuetify 类

如何在启动时滚动 div 的底部?

Angular 以Angular 获取当前聚焦的元素

父母是内联块,子元素有%填充=奇怪的行为

基于变量动态生成 CSS 类 - SCSS

如何断言 CSS 属性包含Cypress 测试中的一些文本?

左右卡片的 OwlCarousel 导航:react js

页脚上方的图像分隔线使用 ::before 伪元素

如何创建一个与 Bootstrap 3 配合得很好的粘性页脚

如何在需要时通过 HTTPS 包含 CSS 和 JS 文件?

填充剩余的垂直空间 - 仅 CSS

div内的CSS中心内容

当子元素水平溢出时,为什么忽略父元素的右填充?

输入上方带有标签的样式表

仅使用 css 的换行符(如
)