CSS可以用来隐藏滚动条吗?你会怎么做?
当前回答
隐藏滚动条的跨浏览器方法。
它在Edge、Chrome、Firefox和Safari上进行了测试
隐藏滚动条,同时仍然可以使用鼠标滚轮滚动!
代码笔
/* Make parent invisible */
#parent {
visibility: hidden;
overflow: scroll;
}
/* Safari and Chrome specific style. Don't need to make parent invisible, because we can style WebKit scrollbars */
#parent:not(*:root) {
visibility: visible;
}
/* Make Safari and Chrome scrollbar invisible */
#parent::-webkit-scrollbar {
visibility: hidden;
}
/* Make the child visible */
#child {
visibility: visible;
}
其他回答
要禁用垂直滚动条,只需添加overflow-y:hidden;。
了解更多信息:溢出。
设置溢出:隐藏;在body标签上,如下所示:
<style type="text/css">
body {
overflow: hidden;
}
</style>
上面的代码“隐藏”了水平和垂直滚动条。
如果要仅隐藏垂直滚动条,请使用overflow-y:
<style type="text/css">
body {
overflow-y: hidden;
}
</style>
如果您只想隐藏水平滚动条,请使用overflow-x:
<style type="text/css">
body {
overflow-x: hidden;
}
</style>
如果需要,内容将被剪裁以适合填充框。不提供滚动条,也不允许用户滚动(例如通过拖动或使用滚轮)。内容可以通过编程方式滚动(例如,通过设置属性值,如offsetLeft),因此元素仍然是滚动容器。(来源)
CSS可以用来隐藏滚动条吗?你会怎么做?
如果要从浏览器视口中删除垂直(和水平)滚动条,请添加:
style="position: fixed;"
到<body>元素。
Java脚本:
document.body.style.position = 'fixed';
CSS:
body {
position: fixed;
}
如果您希望滚动工作,在隐藏滚动条之前,请考虑设置样式他们现代版本的OS X和移动操作系统具有滚动条,而不适合用鼠标抓取,非常漂亮和中性。
为了隐藏滚动条,约翰·库尔拉克(John Kurlak)的一项技术很好,除了离开没有触摸板无法滚动的Firefox用户,除非他们有一只带轮子的老鼠,他们可能会这样做,但即使这样,他们通常也可以仅垂直滚动。
John的技术使用三个元素:
屏蔽滚动条的外部元素。具有滚动条的中间元素。以及一个内容元素,既可以设置中间元素的大小它有滚动条。
必须可以将外部元素和内容元素的大小设置为相同这消除了使用百分比,但我想不出其他什么用正确的调整是行不通的。
我最担心的是是否所有版本的浏览器都设置了滚动条可见溢出内容可见。我已经在当前浏览器中进行了测试,但是而不是更老的。
请原谅我的SASS;P
%size {
// set width and height
}
.outer {
// mask scrollbars of child
overflow: hidden;
// set mask size
@extend %size;
// has absolutely positioned child
position: relative;
}
.middle {
// always have scrollbars.
// don't use auto, it leaves vertical scrollbar showing
overflow: scroll;
// without absolute, the vertical scrollbar shows
position: absolute;
}
// prevent text selection from revealing scrollbar, which it only does on
// some webkit based browsers.
.middle::-webkit-scrollbar {
display: none;
}
.content {
// push scrollbars behind mask
@extend %size;
}
测试
OS X为10.6.8。Windows是Windows 7。
Firefox 32.0隐藏滚动条。箭头键不会滚动,即使在单击聚焦后,但鼠标滚轮和触摸板上的两个手指可以操作OS X和Windows。铬37.0隐藏滚动条。单击以聚焦后,箭头键起作用。鼠标滚轮和轨迹板工作。OS X和Windows。Internet Explorer 11隐藏滚动条。单击以聚焦后,箭头键起作用。鼠标滚轮作品窗户。Safari 5.1.10隐藏滚动条。单击以聚焦后,箭头键起作用。鼠标滚轮和轨迹板工作。操作系统X。Android 4.4.4和4.1.2。隐藏滚动条。触摸滚动工作。在Chrome 37.0、Firefox中试用32.0和4.4.4上的HTMLViewer(无论是什么)。在HTMLViewer中,页面是屏蔽内容的大小,也可以滚动!滚动与页面缩放进行可接受的交互。
除了彼得的回答:
如果您想从iframe中删除滚动条,则需要在iframe网站中添加用于删除滚动条的样式。无法在包含滚动条的iframe中设置元素的样式。
具有iframe的网站:
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Page Title</title>
<body>
<iframe src="/iframe"></iframe>
</body>
</html>
已命名的网站:
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Page Title</title>
<style>
html, body {
margin: 0;
padding: 0
}
.content {
scrollbar-width: none;
}
.content::-webkit-scrollbar {
display: none;
}
.content {
height: 100vh;
overflow: scroll;
}
</style>
<body>
<div class="content">
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</div>
</body>
</html>