我有一个可滚动的div,在我的Mac上的Chrome浏览器中,当我不滚动时,隐藏了滚动条。然而在Windows 8上,它在Chrome和Firefox中无法正常工作。
IE也不支持此功能,但是我已经使用以下CSS启用了它:
-ms-overflow-style: -ms-autohiding-scrollbar;
有没有办法在Chrome和Firefox中启用这种行为?
这是一个jsfiddle
我有一个可滚动的div,在我的Mac上的Chrome浏览器中,当我不滚动时,隐藏了滚动条。然而在Windows 8上,它在Chrome和Firefox中无法正常工作。
IE也不支持此功能,但是我已经使用以下CSS启用了它:
-ms-overflow-style: -ms-autohiding-scrollbar;
有没有办法在Chrome和Firefox中启用这种行为?
这是一个jsfiddle
也许你可以使用类似这样的东西?
body {
margin:0;
padding:0;
overflow-y: hidden;
}
body:hover {
overflow-y: scroll;
}
滚动条在您的 Mac 上被隐藏,因为这是一个系统偏好设置(System Preferences > General > Show scroll bars)。而不幸的是,在Firefox或Chrome中没有-ms-overflow-style
的版本。
<html>
[...]
<div id="scrollable">Some Y large content</div>
[...]
</html>
<html>
[...]
<div id="scrollable-cover">
<div id="scrollable">Some Y large content</div>
</div>
[...]
</html>
并且它的CSS:
#scrollable-cover {
width: 800px;
height: 900px;
overflow: hidden
}
#scrollable {
width: 820px;
height: 100%;
overflow: scroll;
}
因此,#scrollable将被拉伸到其直接父元素(#scrollable-cover)的高度,并且其大量内容将使其呈现为可滚动框,但是,由于其宽度比具有“overflow:hidden”属性的父元素大20像素,因此不会显示滚动条,因为它呈现在#scrollable右侧隐藏的20像素上。
这引导我们面临一个问题,即#scrollable的内容也可能在该隐藏的20像素宽度区域中呈现。为了避免这种情况,有两种方法。 一种是将#scrollable的所有内容包装在具有800px宽度和自动高度的#scrollable-wrapper中:
<html>
[...]
<style>
#scrollable-cover {
width: 800px;
height: 900px;
overflow: hidden
}
#scrollable {
width: 820px;
height: 100%;
overflow: scroll;
}
#scrollable-wrapper {
width: 800px;
height: auto;
}
</style>
[...]
<div id="scrollable-cover">
<div id="scrollable">
<div id="scrollable-wrapper">Some Y large content</div>
</div>
</div>
[...]
</html>
#scrollable-cover {
width: 800px;
height: 900px;
overflow: hidden
}
#scrollable {
width: 820px;
height: 100%;
overflow: scroll;
padding-right: 20px
box-sizing: border-box;
}
对于那些不是因为系统偏好而是在Windows Chrome系统中浏览时发现滚动条一般都可见的人:
不要像这样保存你的css文件:
overflow: scroll;
但是相反
overflow: auto;
这样只有在必要时才会在Windows Chrome浏览器上显示。
在此处找到:隐藏仅在Windows上出现的无用滚动条