简体   繁体   English

如何拉伸使用JS“ document.body.style.backgroundImage = url(imgsrc);”插入的背景图像?

[英]How to stretch a background image thats being inserted with JS “document.body.style.backgroundImage = url(imgsrc);”?

I'm making a page that has the background change based on the time of day. 我正在制作一个根据一天中的时间进行背景更改的页面。 For instance, when hours < 12, it will set to an image like this: 例如,当小时数小于12时,它将设置为以下图像:

document.body.style.backgroundImage = "url(../img/morning.jpg)";

The image displays in the page but its a high resolution image and only shows a small section of the image, so I'm trying to stretch it. 该图像显示在页面中,但是它是高分辨率图像,并且仅显示图像的一小部分,因此我尝试拉伸它。 How do I reference it since it isn't an HTML element / I can't target it with CSS? 由于它不是HTML元素/我不能用CSS定位,因此如何引用它? At least I don't think I can? 至少我认为我做不到?

Thanks 谢谢

Yes you can do it using CSS. 是的,您可以使用CSS进行操作。 Like this: 像这样:

body {
   background-size: cover;
}

将背景尺寸设置为“覆盖”

One way is to use css property as other mentioned before. 一种方法是使用css属性,如前所述。 Another way is to use API for image Resize 另一种方法是使用API调整图像大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM