html - Vertical overflow always showing scrollbar which never works
问题描述
I have a sidebar with a menu with icons. When it overflows vertically you should be able to scroll the menu vertically so I am using overflow-y: scroll
and the parent has overflow-y
auto. However, scrolling is not working at all.
- the scroll bar is always showing, even when the content isn't overflowing (just try removing half of the list items in my fiddle and you'll see what I mean)
- the scroll bar is simply not functional. It appears locked and you can't really drag it to scroll up and down the menu.
Note I've tried with overflow-y: auto
but it doesn't work either.
Also, the last three links should be pushed to be bottom of the sidebar (if there is enough space). For that, I'm using margin-top: auto
on the first item of the group.
HTML:
<div class="sidebar">
<div class="sidebar-head">H</div>
<ul class="menu">
<li><a href="">1</a></li>
<li><a href="">2</a></li>
<li><a href="">3</a></li>
<li><a href="">4</a></li>
<li><a href="">5</a></li>
<li><a href="">6</a></li>
<li class="bottom"><a href="">7</a></li>
<li><a href="">8</a></li>
<li><a href="">9</a></li>
</ul>
</div>
CSS:
.sidebar {
display: flex;
flex-direction: column;
box-shadow: none;
position: fixed;
top: 0;
bottom: 0;
left: 0;
padding-top: 70px;
width: 230px;
overflow-x: visible;
overflow-y: auto;
z-index: 10;
background-color: #222d32;
transition: left 0.3s ease, width 0.3s ease;
left: 0;
width: 50px;
}
.sidebar-head {
display: flex;
align-items: center;
padding: 10px;
margin-bottom: 20px;
color: #fff;
}
.menu {
margin-bottom: 0;
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: flex-start;
flex: 1;
padding-bottom: 10px;
padding-left: 0;
list-style: none;
overflow-x: hidden;
overflow-y: scroll;
}
.menu > li {
width: 100%;
}
.menu > li > a {
position: relative;
display: flex;
align-items: center;
padding: 12px 15px;
font-size: 1.08em;
border-left: 3px solid transparent;
transition: border-left-color 0.3s ease, background-color 0.3s ease;
color: #fff;
background: #0d1214;
border-left-color: #009688;
text-decoration: none;
overflow: hidden;
}
.bottom {
margin-top: auto;
}
解决方案
这是因为你已经给出overflow-y:scroll
了 class 的列表menu
。将其更改为overflow-y:auto
将修复它。
这是更正的小提琴: 小提琴
推荐阅读
- java - 可以将 IntelliJ 配置为在运行时通过反射加载的 Java 类中命中断点吗?
- opencv - 具有自适应背景、静态对象的 OpenCV 前景检测
- c++ - 使用 MSBuild 和 Visual Studio 2012+ 预处理 C++ 文件
- r - R - 总结相对学期序列的课程注册
- java - 如何使用控制器类切换场景,以 ActionEvent 作为参数?
- xml - 如何使用 LET 和 WHERE 获取元素的名称?
- bash - 检查脚本中的目录是否存在会产生错误“不存在”
- terraform - 当 azure 策略要求所有子网上的 NSG 时,如何处理 Terraform 销毁操作?
- python - 如何在条形图上隔开 x 刻度并缩放 y 轴
- javascript - 在 es6、javascript 中使用 rest 参数 concat