我想隐藏我的div
元素和整个body
的任何滚动条,但仍然允许用户使用鼠标滚轮或箭头键进行滚动。如何使用原始JavaScript或jQuery实现?有什么想法吗?
我想隐藏我的div
元素和整个body
的任何滚动条,但仍然允许用户使用鼠标滚轮或箭头键进行滚动。如何使用原始JavaScript或jQuery实现?有什么想法吗?
overflow:hidden
来禁用body/div上的滚动条。
然后,您可以将mousewheel
事件绑定到一个函数,该函数会更改div的scrollTop
以模拟滚动。keydown
事件绑定到识别箭头键的函数上,然后根据需要更改div的scrollTop
和scrollLeft
以模拟滚动。(注意: 由于IE不识别箭头键的keypress
,所以您需要使用keydown
。)
编辑:我无法让FF/Chrome在div上识别keydown
,但在IE8中可以正常工作。根据您的需要,您可以在document
上设置一个keydown
监听器来滚动div。(例如,查看keyCode参考。)<div id="example" style="width:300px;height:200px;overflow:hidden">
insert enough text to overflow div here
</div>
<script>
$("#example").bind("mousewheel",function(ev, delta) {
var scrollTop = $(this).scrollTop();
$(this).scrollTop(scrollTop-Math.round(delta));
});
</script>
(这是一个快速的模拟,您需要根据自己的情况调整数字,因为对我来说,这滚动有点慢。)
keyCode 参考
鼠标滚轮插件
keydown、keypress @ quirksmode
更新于 2012 年 12 月 19 日:
鼠标滚轮插件的更新位置在:https://github.com/brandonaaron/jquery-mousewheel
有没有纯CSS的解决方案呢?
解决方案1(跨浏览器但更加hacky)
#div {
position: fixed;
right: -20px;
left: 20px;
background-color: black;
color: white;
height: 5em;
overflow-y: scroll;
overflow-x: hidden;
}
<html>
<body>
<div id="div">
Scrolling div with hidden scrollbars!<br/>
On overflow, this div will scroll with the mousewheel but scrollbars won't be visible.<br/>
Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>
</div>
</body>
</html>
解决方案2(使用实验性功能,可能不支持某些浏览器)
只需将nobars
类添加到您想要隐藏滚动条的任何元素上即可。
.nobars {
/* Firefox: https://developer.mozilla.org/en-US/docs/Web/CSS/scrollbar-width */
scrollbar-width: none;
/* IE: https://msdn.microsoft.com/en-us/library/hh771902(v=vs.85).aspx */
-ms-overflow-style: none;
}
.nobars::-webkit-scrollbar {
/* Chrome/Edge/Opera/Safari: https://css-tricks.com/custom-scrollbars-in-webkit/ */
display: none;
}
解决方案3(跨浏览器Javascript)
完美滚动条不需要jQuery(虽然可以利用已安装的jQuery),并且有演示可用。组件可以用CSS进行样式设置,例如以下示例:
.ps__rail-y {
display: none !important;
}
<link rel="stylesheet" href="css/perfect-scrollbar.css">
<style>
#container {
position: relative; /* can be absolute or fixed if required */
height: 200px; /* any value will do */
overflow: auto;
}
.ps__rail-y {
display: none !important;
}
</style>
<script src='dist/perfect-scrollbar.min.js'></script>
<div id="container">
Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>Scrollable<br>
</div>
<script>
// on dom ready...
var container = document.getElementById("container");
var ps = new PerfectScrollbar(container);
//ps.update(container);
//ps.destroy(container);
</script>
-moz-scrollbars-none
确实禁用了滚动条,还是有什么方法可以将其与overflow:auto
结合起来使用吗? - phreakhead您不必使用jQuery或JS来实现此功能。使用简单的webkit更具性能优势。
只需将以下代码添加到您的CSS文件中即可。
::-webkit-scrollbar {
display: none;
}
注意!这将禁用所有滚动条,因此请确保将其放在特定的类或ID中,如果您只想隐藏一个滚动条。
我更喜欢SamGoody在这个问题的重复提问中所提供的答案。它保留了本地滚动效果,而不是试图手动重新实现某些特定输入设备的效果:
更好的解决方案是将目标div设置为overflow:scroll,并将其包装在一个比其窄8px的第二个元素中,这个第二个元素的overflow:hidden。
查看原始评论以获取详细示例。您可能需要使用JavaScript确定滚动条的实际大小,而不像他的示例那样假定它们总是8px宽。
html { overflow-y: hidden; }
但是我使用 $(this).scrollTop()
时出现了问题,所以我绑定了我的 #id,但调整了窗口的scrollTop。此外,我的平滑滚动鼠标会触发大量的1或-1增量,因此我将其乘以20。
$("#example").bind("mousewheel", function (ev, delta) {
var scrollTop = $(window).scrollTop();
$(window).scrollTop(scrollTop - Math.round(delta * 20));
});
overflow-x: hidden
。谢谢! - Edward Anderson正如Baldráni上面所说的那样
::-webkit-scrollbar { display: none; }
或者你可以这样做
::-webkit-scrollbar{ width: 0px; }
(为了其他通过谷歌搜索而偶然发现此内容的人发布!)
在我看来,这可能不是最直观的,但我可以想象你能够让它成为一个不错的体验,试试看吧。
overflow:hidden;
请确保父对象具有height
和width
属性,并显示为block