简体   繁体   English

你如何隐藏滚动条但允许滚动?

[英]How do you hide the scrollbar but allow scrolling?

I want to make my own scrollbar but to get that done I first need to hide the default scrollbar but allow scrolling. 我想创建自己的滚动条,但为了完成这项工作,我首先需要隐藏默认滚动条但允许滚动。

I already tried: 我已经尝试过:

overflow-y:hidden;

but than you hide the scrollbar and disable scrolling. 但是你隐藏滚动条并禁用滚动。

I know it there are other people who asked this questions here but I don't want to just at a padding to hide it. 我知道还有其他人在这里提出这个问题,但我不想只是在填充物中隐藏它。 I want a way tho hide it completly. 我想要一种完全隐藏它的方法。

you dont have to allow manual scrolling, you could scroll programatically. 您不必允许手动滚动,您可以以编程方式滚动。 just get your custom scrollbar's position and scroll the content accordingly... 只需获取自定义滚动条的位置并相应滚动内容...

here is a short example with buttons: 这是一个带按钮的简短示例:

 document.getElementById("right").addEventListener("mousemove",function(){ document.getElementById("outer").scrollLeft+=10; }); document.getElementById("left").addEventListener("mousemove",function(){ document.getElementById("outer").scrollLeft-=10; }); 
 #outer { float:left; border:2px solid red; height:100px; width:200px; display:inline-block; overflow-x:hidden; } #inner { display:block; height:100%; white-space:nowrap; } #left { float:left; border:2px solid red; height:100px; width:50px; display:inline-block; background:lime; line-height:100px; } #right { float:left; border:2px solid red; height:100px; width:50px; display:inline-block; background:lime; line-height:100px; } 
 <div id="left">&lt--</div> <div id="outer"> <div id="inner">sdk hfkhsdbf khsdbf kbsdf kbsdkjf sdkjbg lsdkbg;kSBGKdbs gksbd gksdb g</div> </div> <div id="right">--&gt</div> 

您可以隐藏滚动条,但允许使用以下属性在Firefox中滚动:

scrollbar-width: none;

The best way to do this is using a jQuery plugin. 最好的方法是使用jQuery插件。

The most customizable and flexible one is jScrollPane which works for both vertical and horizontal scrolling and allows you to easily style it using CSS by applying styles to elements like handle, rail, wrapper etc. Note that scrollbals do require styling as they are quite ugly out of the box. 最可定制和灵活的是jScrollPane ,它适用于垂直和水平滚动,并允许您通过将样式应用于句柄,轨道,包装等元素,使用CSS轻松地设置样式。请注意,滚动条确实需要样式,因为它们非常难看的盒子。

If you want something prettier try perfect-scrollbar which nicely mimics the behaviours of scrollbars to look like those on Mac OS X (nice and thing, visible only when needed). 如果你想要更漂亮的东西尝试完美滚动条 ,它很好地模仿滚动条的行为看起来像Mac OS X上的那些(好的和东西,仅在需要时可见)。

I've also used jQuery-slimScroll and it also did the job very well. 我也使用过jQuery-slimScroll ,它也很好地完成了这项工作。 Lightweight, easy to use, good looking plus easy to style, but not as customizeable as the first one. 轻巧,易于使用,外观美观,易于设计,但不像第一个那样可定制。

It is theoreticaly also possible to just hide the scrollbar using CSS only, but this is hacky and very bad for user experience. 理论上也可以仅使用CSS隐藏滚动条,但这对于用户体验来说非常糟糕且非常糟糕。 I would definetly go with a jQuery plugin solution as they will deal with default scrollbars all by themselves. 我肯定会使用jQuery插件解决方案,因为他们将自己处理默认滚动条。

Here's the CSS: 这是CSS:

::-webkit-scrollbar {
    height: 0px !important;
    width: 0px !important;
    display: none !important;
}

Also see the Developer Documentation on webkit.org. 另请参阅webkit.org上的开发人员文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM