Hiding the scroll bar on an HTML page

前端 未结 21 1093
旧巷少年郎
旧巷少年郎 2020-11-22 00:07

Can CSS be used to hide the scroll bar? How would you do this?

相关标签:
21条回答
  • 2020-11-22 01:05

    Use the CSS overflow property:

    .noscroll {
      width: 150px;
      height: 150px;
      overflow: auto; /* Or hidden, or visible */
    }
    

    Here are some more examples:

    • overflow-x, overflow-y tests
    • The CSS Overflow Property
    0 讨论(0)
  • 2020-11-22 01:07

    WebKit supports scrollbar pseudo elements that can be hidden with standard CSS rules:

    #element::-webkit-scrollbar {
        display: none;
    }
    

    If you want all scrollbars hidden, use

    ::-webkit-scrollbar {
        display: none;
    }
    

    I'm not sure about restoring - this did work, but there might be a right way to do it:

    ::-webkit-scrollbar {
        display: block;
    }
    

    You can of course always use width: 0, which can then be easily restored with width: auto, but I'm not a fan of abusing width for visibility tweaks.

    Firefox 64 now supports the experimental scrollbar-width property by default (63 requires a configuration flag to be set). To hide the scrollbar in Firefox 64:

    #element {
        scrollbar-width: none;
    }
    

    To see if your current browser supports either the pseudo element or scrollbar-width, try this snippet:

    .content {
      /* These rules create an artificially confined space, so we get
         a scrollbar that we can hide. They are not directly involved in
         hiding the scrollbar. */
    
      border: 1px dashed gray;
      padding: .5em;
    
      white-space: pre-wrap;
      height: 5em;
      overflow-y: scroll;
    }
    
    .content {
      /* This is the magic bit for Firefox */
      scrollbar-width: none;
    }
    
    .content::-webkit-scrollbar {
      /* This is the magic bit for WebKit */
      display: none;
    }
    <div class='content'>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eu
    urna et leo aliquet malesuada ut ac dolor. Fusce non arcu vel ligula
    fermentum sodales a quis sapien. Sed imperdiet justo sit amet venenatis
    egestas. Integer vitae tempor enim. In dapibus nisl sit amet purus congue
    tincidunt. Morbi tincidunt ut eros in rutrum. Sed quam erat, faucibus
    vel tempor et, elementum at tortor. Praesent ac libero at arcu eleifend
    mollis ut eget sapien. Duis placerat suscipit eros, eu tempor tellus
    facilisis a. Vivamus vulputate enim felis, a euismod diam elementum
    non. Duis efficitur ac elit non placerat. Integer porta viverra nunc,
    sed semper ipsum. Nam laoreet libero lacus.
    
    Sed sit amet tincidunt felis. Sed imperdiet, nunc ut porta elementum,
    eros mi egestas nibh, facilisis rutrum sapien dolor quis justo. Quisque
    nec magna erat. Phasellus vehicula porttitor nulla et dictum. Sed
    tincidunt scelerisque finibus. Maecenas consequat massa aliquam pretium
    volutpat. Duis elementum magna vel velit elementum, ut scelerisque
    odio faucibus.
    </div>


    (Note that this is not really a correct answer to the question, because it hides the horizontal bars as well, but that's what I was looking for when Google pointed me here, so I figured I'd post it anyway.)

    0 讨论(0)
  • 2020-11-22 01:09

    I wrote a WebKit version with some options like auto hide, little version, scroll only-y, or only-x:

    ._scrollable{
        @size: 15px;
        @little_version_ratio: 2;
        @scrollbar-bg-color: rgba(0,0,0,0.15);
        @scrollbar-handler-color: rgba(0,0,0,0.15);
        @scrollbar-handler-color-hover: rgba(0,0,0,0.3);
        @scrollbar-coner-color: rgba(0,0,0,0);
    
        overflow-y: scroll;
        overflow-x: scroll;
        -webkit-overflow-scrolling: touch;
        width: 100%;
        height: 100%;
    
        &::-webkit-scrollbar {
            background: none;
            width: @size;
            height: @size;
        }
    
        &::-webkit-scrollbar-track {
            background-color:@scrollbar-bg-color;
            border-radius: @size;
        }
    
        &::-webkit-scrollbar-thumb {
            border-radius: @size;
            background-color:@scrollbar-handler-color;
            &:hover{
                background-color:@scrollbar-handler-color-hover;
            }
        }
    
        &::-webkit-scrollbar-corner {
          background-color: @scrollbar-coner-color;
        }
    
        &.little{
            &::-webkit-scrollbar {
                background: none;
                width: @size / @little_version_ratio;
                height: @size / @little_version_ratio;
            }
            &::-webkit-scrollbar-track {
                border-radius: @size / @little_version_ratio;
            }
            &::-webkit-scrollbar-thumb {
                border-radius: @size / @little_version_ratio;
            }
        }
    
        &.autoHideScrollbar{
            overflow-x: hidden;
            overflow-y: hidden;
            &:hover{
                overflow-y: scroll;
                overflow-x: scroll;
                -webkit-overflow-scrolling: touch;
                &.only-y{
                    overflow-y: scroll !important;
                    overflow-x: hidden !important;
                }
    
                &.only-x{
                    overflow-x: scroll !important;
                    overflow-y: hidden !important;
                }
            }
        }
    
        &.only-y:not(.autoHideScrollbar){
            overflow-y: scroll !important;
            overflow-x: hidden !important;
        }
    
        &.only-x:not(.autoHideScrollbar){
            overflow-x: scroll !important;
            overflow-y: hidden !important;
        }
    }
    

    http://codepen.io/hicTech/pen/KmKrjb

    0 讨论(0)
提交回复
热议问题