代码之家  ›  专栏  ›  技术社区  ›  revy

CSS:使滚动条始终可见而不减少容器的宽度

  •  0
  • revy  · 技术社区  · 7 年前

    我试图在一个垂直可滚动的div中有一个始终可见的滚动条,但是没有滚动条缩小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 回复  |  直到 7 年前
        1
  •  1
  •   nolawi    7 年前

    如果您特别希望它与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

    推荐文章