Appearance
iframe 隐藏滚动条
查了好久的文档,研究了好久,终于解决了把 iframe 的滚动条隐藏的办法了。 有以下两种办法。
1. 直接隐藏的策略:
frame 有个属性 scrolling,直接设置 scrolling='no' 即可隐藏 scrollbar。但是这样做的话就不能滚动了。可能不是我们想要的。
html
<iframe src="xxx" scrolling="no"></iframe>2. 修改 iframe 内部的样式
主要是外部的样式无法影响 iframe 内部,本来隐藏主 html 的滚动条的话,只需要设置一下 -webkit-scrollbar 就可以了。
html
::-webkit-scrollbar { width: 0; /* Remove scrollbar space */ background:
transparent; /* Optional: just make scrollbar invisible */ }相应的,给 iframe 内部的 body 增加这个样式的话就能隐藏 iframe 的滚动条了。因此增加以下代码即可。
javascript
function addCSS(css) {
var style = document.createElement('style')
if (style.styleSheet) {
// IE
style.styleSheet.cssText = css
} else {
// W3C
style.appendChild(document.createTextNode(css))
}
for (let frame of document.getElementsByTagName('iframe')) {
frame.contentWindow.document.body.append(style)
}
}
// 使用函数来添加一些CSS
addCSS(`
::-webkit-scrollbar {
width: 0; /* Remove scrollbar space */
background: transparent; /* Optional: just make scrollbar invisible */
}
`)