Showing scrollbars only when mouseover div

后端 未结 6 1981
猫巷女王i
猫巷女王i 2020-12-31 02:22

Given this div:

How can I make the scrollbars visible only when the mouse is

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 03:06

    I came up with this solution. Basically the negative margin cuts off the vertical scrollbar.

    .hidden-scrollbar {
        padding-right: 50px;
        margin-right: -25px;        
        overflow-y: auto;        
    }
    
    .hidden-scrollbar.hover-scrollbar:hover {
        padding-right: 25px;
        margin-right: 0;
        overflow-y: auto;
    }
    

    LESS mixin

    .hidden-scrollbar(@padding) {
        padding-right: 2 * @padding;
        margin-right: -@padding;
        overflow-y: auto;        
    
        &.hover-scrollbar:hover {
            padding-right: @padding;
            margin-right: 0;
        }
    }
    

    NOTE: @padding should be at least the scrollbar width (e.g. 25px)

    Basically add this to your LESS/CSS and add the class to the element that needs it's scrollbar cut off.

提交回复
热议问题