Hide scroll bar, but while still being able to scr

2018-12-30 23:55发布

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: none; 
}

But Mozilla Firefox and Internet Explorer don't seem to work like that.

I also tried this in CSS:

overflow: hidden;

That does hide the scrollbar, but I can't scroll anymore.

Is there any way I can remove the scrollbar while still being able to scroll the whole page? With just CSS or HTML, please.

24条回答
与君花间醉酒
2楼-- · 2018-12-31 00:49

HTML:

<div class="parent">
    <div class="child">
    </div>
</div>

CSS:

.parent{
    position: relative;
    width: 300px;
    height: 150px;
    border: 1px solid black;
    overflow: hidden;
}

.child {
    height: 150px;   
    width: 318px;
    overflow-y: scroll;
}

Apply CSS accordingly.

Check it here (tested in IE and FF).

查看更多
何处买醉
3楼-- · 2018-12-31 00:55

This works for me:

.container {
    -ms-overflow-style: none;  // IE 10+
    overflow: -moz-scrollbars-none;  // Firefox
}
.container::-webkit-scrollbar { 
    display: none;  // Safari and Chrome
}

Note: In the latest versions of Firefox the -moz-scrollbars-none property is deprecated ( link ).

查看更多
皆成旧梦
4楼-- · 2018-12-31 00:56

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.

查看更多
与君花间醉酒
5楼-- · 2018-12-31 00:56

Not sure if I'm too late to the party but adding

    overflow: -moz-scrollbars-none;

worked for me

查看更多
大哥的爱人
6楼-- · 2018-12-31 00:57

This is a divitis-esque solution which nontheless should work for all browsers...

The markup is as follows, and needs to be inside something with relative positioning (and its width should be set, for example 400px):

<div class="hide-scrollbar">
    <div class="scrollbar">
        <div class="scrollbar-inner">

        </div>
    </div>
</div>

The CSS:

.hide-scrollbar {
    overflow: hidden;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

.scrollbar {
    overflow-y: scroll;
    position: absolute;
    top: 0;
    left: 0;
    right: -50px;
    bottom: 0;
}

.scrollbar-inner {
    width: 400px;
}
查看更多
骚的不知所云
7楼-- · 2018-12-31 00:57

perfect-scrollbar plugin seems to be the way to go, see: https://github.com/noraesae/perfect-scrollbar

It is well documented and complete JS based solution for the scrollbars issue.

Demo page: http://noraesae.github.io/perfect-scrollbar/

查看更多
登录 后发表回答