CSS webkit scrollbar show/hide

后端 未结 5 1004
萌比男神i
萌比男神i 2021-01-31 21:47

I\'m using -webkit-scrollbar and what I want to happen is the scrollbar hidden on page load, and it stays hidden until you hover over the container div it is attached to. When y

5条回答
  •  轮回少年
    2021-01-31 22:52

    Hiding the scrollthumb in webkit is non-intuitive! My page needed to hide all default browser scroll features in order to implement our own 'infinite scroll' effects. The only thing that was hard was the webkit "thumb" overlay, which only shows up while the screen is in motion (like scrolling with the mousewheel).

    In order to hide this webkit scrollthumb we had to apply some styles to "all" scrollthumbs, and then apply the hiding effects to my specific thumbs (we have to do this programmatically because we're not sure if the content is long enough to do our custom scroll effects until the page has loaded).

    These are the styles we used:

    ::-webkit-scrollbar { /* required, although an apparent no-op */
        color: inherit;
    }
    ::-webkit-scrollbar-thumb { /* leave most bars alone */
        background-color: grey;
    }
    #customScrollDiv::-webkit-scrollbar-thumb { /* this is the one we care about */
        background-color: grey;
    }
    .hideThumb::-webkit-scrollbar-thumb { /* we apply the style to actually hide it*/
        display: none; 
    }       
    .hideThumb { /* required, although an apparent no-op */
        background-color: white;
    }
    

    then, when i determine that i want to programmatically hide that thumbHandle i can do it with jquery: $('#customScrollDiv').toggleClass('hideThumb');

    The tricky part is that you need a lot of "defaulting" of CSS styles that you normally got out of the box, but once you start specifying even one of these webkit styles above then you need THEM ALL or they won't be applied.

提交回复
热议问题