Hide scroll bar, but while still being able to scroll

前端 未结 30 3465
悲哀的现实
悲哀的现实 2020-11-21 04:22

I want to be able to scroll through the whole page, but without the scrollbar being shown.

In Google Chrome it\'s:

::-webkit-scrollbar {
    display:         


        
30条回答
  •  时光取名叫无心
    2020-11-21 05:02

    Another sort of hacky approach is to do overflow-y: hidden and then manually scroll the element with something like this:

    function detectMouseWheelDirection(e) {
      var delta = null, direction = false;
      if (!e) { // If the event is not provided, we get it from the window object
        e = window.event;
      }
      if (e.wheelDelta) { // Will work in most cases
        delta = e.wheelDelta / 60;
      } else if (e.detail) { // Fallback for Firefox
        delta = -e.detail / 2;
      }
      if (delta !== null) {
        direction = delta > 0 ? -200 : 200;
      }
      return direction;
    }
    
    if (element.addEventListener) {
      element.addEventListener('DOMMouseScroll', function(e) {
        element.scrollBy({
          top: detectMouseWheelDirection(e),
          left: 0,
          behavior: 'smooth'
        });
      });
    }
    

    There's a great article about how to detect and deal with onmousewheel events in deepmikoto's blog. This might work for you, but it is definitively not an elegant solution.

提交回复
热议问题