html - 在 HTML 中包围整个主体的元素的属性

标签 html css google-chrome-devtools

我有一个页面,我正在使用 chrome 控制台添加一个覆盖整个页面的 SVG 元素。 我在控制台中输入了以下命令,

var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.style.position = "absolute";
svg.style.left = svg.style.right = svg.style.top = svg.style.bottom = "0px";
svg.style.opacity = 0.5;
svg.style['z-index'] = 100;
svg.style.width = "100%";
svg.style.height = "100%";
document.body.appendChild(svg);

但是 SVG 元素只覆盖页面显示的部分,即如果页面足够长以至于我们必须向下滚动,则不会覆盖整个页面。

有什么建议吗?

最佳答案

如果您想在视口(viewport)处固定元素,请改用position:fixed;

否则,不要使用100%,而是:

svg.style.height = Math.max(document.body.scrollHeight, 
    document.documentElement.scrollHeight) + 'px';

关于html - 在 HTML 中包围整个主体的元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11429057/

相关文章:

google-chrome - Chrome 开发工具控制台完全空白

performance - LightHouse 和 Performance 之间的 LCP 时间 - Google Chrome

javascript - 当与 attr 函数一起使用时,javascript 数组中的元素输出未定义而不是实际值

css - 某些类型的 css 选择器的浏览器兼容性

html - CSS:突出显示当前菜单项

javascript - 当我在 IE 的 View 中聚焦 dom 时,body scrollTop 发生了变化

css - Chrome Web Developer 工具,实时 CSS 编辑不起作用,重置为默认值

javascript - D3 折线图显示为面积图

javascript - 循环遍历 DOM 元素,包括 span 元素

html - 静态 header (包括 NAV 和其他 DIV)