1 回答
TA贡献1891条经验 获得超3个赞
1.在需要滚动的层scrollbar外再加一个层hide-scrollbar。
2.然后将这个层hide-scrollbar的overflow:hidden。
3.将层scrollbar的position:absolute,并且把需要滚动的那一边的大小设置刚好滚动条的大小(用calc实现)。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
<title>Document</title>
<style>
body {
max-width: 325px;
padding: 10px 30px;
}
.hide-scrollbar {
position: relative;
width: 100%;
height: 100px;
overflow: hidden;
}
.scrollbar {
position: absolute;
width: calc(100% + 18px);
height: 100%;
background-color: #ccc;
overflow-y: auto;
}
.scrollbar ul {
margin: 0;
}
.scrollbar.row {
width: 100%;
height: calc(100% + 18px);
overflow-y: hidden;
overflow-x: auto;
}
.scrollbar.row ul {
white-space: nowrap;
}
.scrollbar.row ul li {
display: inline-block;
line-height: 100px;
}
</style>
</head>
<body>
<h4>竖向</h4>
<div class="hide-scrollbar">
<div class="scrollbar">
<ul>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
</ul>
</div>
</div>
<h4>横向</h4>
<div class="hide-scrollbar">
<div class="scrollbar row">
<ul>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
<li>demo-list</li>
</ul>
</div>
</div>
</body>
</html>
添加回答
举报