CSS可以用来隐藏滚动条吗?你会怎么做?
当前回答
除了彼得的回答:
#element::-webkit-scrollbar {
display: none;
}
这对Internet Explorer 10的作用相同:
#element {
-ms-overflow-style: none;
}
其他回答
这是我的解决方案,理论上涵盖了所有现代浏览器:
html {
scrollbar-width: none; /* For Firefox */
-ms-overflow-style: none; /* For Internet Explorer and Edge */
}
html::-webkit-scrollbar {
width: 0px; /* For Chrome, Safari, and Opera */
}
html可以替换为任何想要隐藏滚动条的元素。
注意:我已经浏览了其他19个答案,看看我发布的代码是否已经被覆盖,似乎没有一个答案能概括2019年的情况,尽管其中很多都非常详细。如果这是别人说的,而我错过了,我深表歉意。
使用CSS溢出属性:
.noscroll {
width: 150px;
height: 150px;
overflow: auto; /* Or hidden, or visible */
}
以下是更多示例:
溢出-x,溢出-y测试CSS溢出属性
除了彼得的回答:
如果您想从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>
我只是想告诉其他读到这个问题的人,在body元素上设置overflow:hidden(或overflow-y)并没有为我隐藏滚动条。
我不得不使用html元素。
隐藏滚动条的跨浏览器方法。
它在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;
}