简体   繁体   English

如何优化背景图像类似于 <picture> 标签?

[英]How to optimize background-image similar to <picture> tag?

I make a website and download speed is extremely important, especially for mobile devices. 我建立了一个网站,下载速度极其重要,尤其是对于移动设备。 How can I optimize the loading of images as we do with tag, but for background-image in 'style' attribute? 如何像使用标签一样优化图像的加载,但是如何针对'style'属性中的background-image?

you can use javascript.. 您可以使用javascript。

get width/height of screen with java script, put it in variable and do if statements and then set styling of element. 使用Java脚本获取屏幕的宽度/高度,将其放入变量中,然后执行if语句,然后设置元素的样式。

var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;

if(width>700){
document.getElementById("id").style.background="url('image.png')";
}

u add as many if statements as you need, similar to @meda queries u根据需要添加任意多的if语句,类似于@meda查询

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

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