最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hide scrollbar with overflow:scroll enabled - Stack Overflow

programmeradmin2浏览0评论

I need to hide the scrollbar on a div that has overflow:scroll; enabled so that the div will scroll with mouse and keyboard but the scrollbar itself will not be displayed.

is there a way of doing this with css or is javascript the way to go?

I need to hide the scrollbar on a div that has overflow:scroll; enabled so that the div will scroll with mouse and keyboard but the scrollbar itself will not be displayed.

is there a way of doing this with css or is javascript the way to go?

Share Improve this question edited Feb 1, 2015 at 16:40 Deduplicator 45.7k7 gold badges72 silver badges123 bronze badges asked Dec 3, 2012 at 13:36 StephenJRomeroStephenJRomero 1782 gold badges2 silver badges9 bronze badges 2
  • 1 Please refrain from changing or removing standard UI elements from your users, it wil only annoy them. – Damien Commented Dec 3, 2012 at 13:38
  • 1 I think people are reading this wrong I don't want to hide the body scroll or any default UI. I have set a div to have overflow:scroll; I want the functionality overflow scroll gives that div but i dont want the scrollbar that comes with it to be displayed. – StephenJRomero Commented Dec 3, 2012 at 13:50
Add a comment  | 

5 Answers 5

Reset to default 32

You can do this with pure CSS (at least in webkit browsers). You have to use special scrollbar pseudo-classes to achieve this

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

Read this excellent blogpost for further information.

You could put the scrolling div inside of a second div with overflow hidden, then just make the inner div a little wider and taller (the amount may vary depending on the browser, however).

Something like this:

#outer {
    overflow:hidden;
    width:200px; 
    height:400px;
    border:1px solid #ccc;
}
#inner {
    overflow:scroll; 
    width:217px; 
    height:417px;
}​

Full example at http://jsfiddle.net/uB6Dg/1/.

Edit: Unfortunately you can still get to the scrollbars by highlighting the text and dragging, and it does make padding etc a bit more of a pain, but other than this I think javascript is the way to go.

@Maloric answer pointed me in the correct direction, however I needed fluid width, and I also wanted to be more accurate on the width of the scrollbar.

Here is a function that will return the exact width of the scrollbar based on what the browser reports.

var getWidth = function () {
    var scrollDiv = document.createElement('div'),
        scrollbarWidth;

    scrollDiv.style.overflow = 'scroll';
    document.body.appendChild(scrollDiv);

    scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
    
    document.body.removeChild(scrollDiv);

    return scrollbarWidth;
};

var width = getWidth();
var container = document.querySelector('.overflowing-container');

container.style.paddingRight = width + 'px';
container.style.marginRight = (width * -1) + 'px';

// Just for testing purposes
document.querySelector('.scrollbar-width').innerHTML = 'scrollbar height: ' + getWidth()
.container {
  height: 200px;
  overflow-x: hidden;
  overflow-y: auto;
  width: 500px;
}

.overflowing-container {
  height: 100%;
  overflow-y: auto;
  width: 100%;

}
<div class="container">
  <div class="overflowing-container">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris tristique feugiat metus, eget mollis nibh vestibulum eu. Nullam eros orci, gravida eu quam nec, maximus posuere dui. Maecenas erat magna, elementum eget nunc eget, tincidunt varius nisl. Phasellus pretium congue consectetur. Donec rutrum nisi sed eros posuere, vel pretium nunc viverra. Praesent consequat sagittis urna, quis convallis magna gravida et. In sed eleifend arcu.

  Duis ornare condimentum est luctus malesuada. Morbi nec sodales nunc. Morbi vehicula tristique massa, nec lacinia tellus vulputate fringilla. Nam eget pulvinar libero. Vestibulum ligula mi, tincidunt ac pellentesque vitae, convallis eu tortor. Cras varius dolor sit amet libero rhoncus, mattis aliquet augue porttitor. Etiam sollicitudin, sem ut mollis imperdiet, erat enim gravida tortor, et imperdiet sem nibh in ex. Aliquam ac aliquam risus. Suspendisse gravida suscipit sapien, et ultrices massa ornare eget. Nulla venenatis pellentesque arcu at auctor. Sed libero ligula, pretium in metus a, malesuada ullamcorper leo. Vivamus tempor velit in ante fringilla rhoncus. Nam ac iaculis arcu. Mauris a nisi quis arcu feugiat posuere.
  </div>
</div>

<div class="scrollbar-width"></div>

The above snippet shows this in action.

set the width of scroll Bar to none:

scrollbar-width: none;

You need to make use of the jquery plugin from this site http://jscrollpane.kelvinluck.com/

发布评论

评论列表(0)

  1. 暂无评论