Javascript 隐藏滚动条,同时仍然可以使用鼠标/键盘滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3293650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:04:09  来源:igfitidea点击:

Hide scrollbar while still being able to scroll with mouse/keyboard

javascriptjqueryhtmlcss

提问by Arthur

Possible Duplicate:
How to disable browser or element scrollbar, but allow scrolling with wheel or arrow keys?

可能的重复:
如何禁用浏览器或元素滚动条,但允许使用滚轮或箭头键滚动?

I want to know if it is possible to hide the scrollbar, while scrolling is still enabled with mouse/keyboard.

我想知道是否可以隐藏滚动条,同时鼠标/键盘仍然启用滚动。

I tried to use CSS: overflow: hidden;. The effect is scrollbar disabled andscrolling disabled.

我尝试使用 CSS: overflow: hidden;。效果是禁用滚动条禁用滚动。

回答by honzzz

For future reference there is also a solution without jQuery - just have the wrapper div style contain overflow:hiddenand use this JavaScript two-liner:

为了将来参考,还有一个没有 jQuery 的解决方案 - 只需包含包装 div 样式overflow:hidden并使用这个 JavaScript 两行:

// get the width of the textarea minus scrollbar
var textareaWidth = document.getElementById("textarea").scrollWidth;

// width of our wrapper equals width of the inner part of the textarea
document.getElementById("wrapper").style.width = textareaWidth + "px";

See demoor complete HOWTO.

请参阅演示或完整的HOWTO

Update: you can use the same principle to create scrollable div without scrollbar: demo.

更新:您可以使用相同的原理创建没有滚动条的可滚动 div:demo

回答by Peter ?rneholm

There is a jQuery plugin called Scrollablethat does what you want.

有一个名为Scrollable的 jQuery 插件可以满足您的需求。

回答by Josh Stodola

Not natively, but I suppose you could implement your own scrolling mechanism by using Javascript to capture certain keystrokes and re-position the container element accordingly with CSS. It's going to be ugly, though!

不是原生的,但我想您可以通过使用 Javascript 来捕获某些击键并使用 CSS 相应地重新定位容器元素来实现自己的滚动机制。不过会很丑!