我试图在垂直可滚动的div中有一个始终可见的滚动条,但没有使滚动条减小div中元素的宽度。 基本上,我希望使用div使用“ overflow-y:scroll”的div具有浏览器的默认行为,但是在不滚动时滚动条不会消失。
我尝试了以下操作,但是如您所见,滚动条减小了div的可用宽度,
这可能吗?
/* Css Reset */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/* ------------------------------------------------------ */
body {
width: 100%;
height: 10%;
background: #ddd;
padding: 2em;
}
#content {
width: 400px;
height: 12em;
background: red;
overflow-y: scroll;
}
#content::-webkit-scrollbar {
border-radius: 0.5em;
height: 4px;
width: 0.5em;
background: transparent;
}
#content::-webkit-scrollbar-thumb {
background: green;
border-radius: 0.5em;
}
#content::-webkit-scrollbar-track {
border-radius: 0.5em;
background-color: transparent;
}
.item {
width: 100%;
height: 4em;
line-height: 4em;
text-align: center;
vertical-align: middle;
color: white;
background: #333;
border-bottom: 1px solid white;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>scrollbar</title>
<meta name="viewport" content="width=device-width initial-scale=1.0 maximum-scale=1.0 user-scalable=yes" />
</head>
<body>
<div id="content">
<ul>
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
</ul>
</div>
</body>
</html>
答案 0 :(得分:1)
如果您特别希望它可以与webkit psuedo类一起使用,那么您将需要采用这种方式
.scrollbar {
margin-left: 22px;
float: left;
height: 180px;
width: 200px;
background: #F5F5F5;
overflow-y: scroll;
margin:10px;
}
.force-overflow {
min-height: 450px;
}
#wrapper {
text-align: center;
margin: auto;
}
#style-1::-webkit-scrollbar {
width: 20px;
}
/** STYLE A */
#style-1::-webkit-scrollbar-thumb {
border-radius: 10px;
background-color: green;
}
#style-1::-webkit-scrollbar-track {
border-radius: 10px;
background-color: red;
}
<div id="wrapper">
<div class="scrollbar" id="style-1">
<div class="force-overflow"><h2>A</h2></div>
</div>
</div>
<div id="wrapper">
<div class="scrollbar" id="style-default">
<div class="force-overflow"><h2>B</h2></div>
</div>
</div>
请注意,-webkit-scrollbar