首页 > 解决方案 > 将 backgroundSize 设置为 window.innerWidth 和 window.innerHeight

问题描述

我正在努力使我的背景图像的高度和宽度为窗口大小的 100%。

这适用于宽度部分:

var ht = document.querySelector("html");

ht.style.background = "url('treebg.jpg')";

ht.style.backgroundSize = window.innerWidth + "px";

当我添加高度时它不起作用:

var ht = document.querySelector("html");

ht.style.background = "url('treebg.jpg')";

ht.style.backgroundSize = window.innerWidth + "px" +

window.innerHeight + "px";

谢谢你的时间。

标签: javascripthtmlcss

解决方案


在第一个“px”之后添加一个空格。

var ht = document.querySelector("html");

ht.style.background = "url('treebg.jpg')";

ht.style.backgroundSize = window.innerWidth + "px " + window.innerHeight + "px";

推荐阅读