[英]How can I prevent images that are on my desktop site from loading on mobile?
So I currently have a website that loads two different versions depending on screen size: one for desktop and one for mobile.所以我目前有一个网站,根据屏幕大小加载两个不同的版本:一个用于桌面,一个用于移动。 For some reason, the images that I display on the desktop version of my site load on the mobile version.
出于某种原因,我在网站桌面版上显示的图像会加载到移动版上。
Is there a way no prevent these images from loading on mobile to increase the performance/load time?有没有办法阻止这些图像在移动设备上加载以增加性能/加载时间? Im not talking about the display:none;
我不是在谈论显示:无; attribute/hiding the image on mobile, I'm trying to prevent them from even loading all together.
在移动设备上属性/隐藏图像,我试图阻止它们甚至一起加载。
If you want to take a look at the site heres the link: https://rubyredsound.com如果您想查看该网站,请访问以下链接: https : //rubyredsound.com
Is there a way to prevent these images from loading on mobile to increase the performance/load time?
有没有办法阻止这些图像加载到移动设备上以增加性能/加载时间?
Yes.是的。
The srcset
attribute is explicitly intended for downloading different image files depending on context. srcset
属性明确用于根据上下文下载不同的图像文件。
The one caveat is that you need to declare an image (or a fallback) for every environment .一个警告是您需要为每个 environment 声明一个图像(或后备) 。
But that won't prevent you from using as your fallback an equivalent-to-null image (ie. a 1x1 pixel transparent png) declared inline using a base-64 Data URL
, which looks like this:但这不会阻止您使用使用
base-64 Data URL
内联声明的等效空图像(即 1x1 像素透明 png)作为回退,如下所示:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==
srcset
and sizes
syntax: srcset
和sizes
语法: The finished <img>
element, with files indicated for desktop and mobile environments will look like something like this:完成的
<img>
元素,以及为桌面和移动环境指示的文件,看起来像这样:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
srcset="/my-image-folder/desktop.png 640w,
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg== 1w"
sizes="(min-width: 641px) 640px,
(max-width: 640px) 1px"
alt="Alternative Text for My Image"
/>
This <img>
element will only download and display the desktop.png
image if the browser viewport width is 641px
or greater.如果浏览器视口宽度为
641px
或更大,此<img>
元素将仅下载并显示desktop.png
图像。
Otherwise it will display a 1x1
pixel transparent png.否则它会显示一个
1x1
像素的透明 png。
Working Example:工作示例:
<h2>Narrow and Widen your Browser Viewport in Full Page</h2> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" srcset="https://via.placeholder.com/620x100 620w, data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg== 1w" sizes="(min-width: 641px) 620px, (max-width: 640px) 1px" alt="Alternative Text for My Image" />
For contemporary browser support for srcset
and sizes
attributes, see:对于当代浏览器支持
srcset
和sizes
的属性,请参阅:
For more in-depth info and a tutorial on using srcset
and sizes
to build Responsive Images
, see:有关使用
srcset
和sizes
构建Responsive Images
更深入信息和教程,请参阅:
The Data URL used in the answer above:上面答案中使用的数据 URL:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==
works, but it's a bit scrappy and pretty unclear what it's supposed to represent (unless you already know what it is).有效,但它有点杂乱且不清楚它应该代表什么(除非你已经知道它是什么)。
I've since realised that:从那以后我意识到:
data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'/%3E
works just as well and is both shorter and cleaner and more human-readable.效果也一样,而且更短、更干净、更易读。
Via CSS you can prevent background images from loading if not active on page load.如果在页面加载时未处于活动状态,您可以通过 CSS 阻止加载背景图像。 Keeping that in mind you can hide an image (or any element really) from mobile on and start displaying them through a Media Query.
记住这一点,您可以在移动设备上隐藏图像(或任何元素)并开始通过媒体查询显示它们。
.myMobileImage {
background-image: url(...);
}
.myDesktopImage {
display: none;
}
@media only screen and (min-width: 768px) {
.myMobileImage {
display: none;
}
.myDesktopImage {
display: inline-block;
background-image: url(...);
}
}
As for personal preference I keep the mobile first approach in mind, declaring mobile styles first and overwriting them with Media Queries afterwards for bigger resolutions.至于个人偏好,我牢记移动优先的方法,首先声明移动样式,然后用媒体查询覆盖它们以获得更大的分辨率。
I see you added JQuery tagg so :我看到你添加了 JQuery 标签,所以:
<figure class="image"></figure>
$(document).ready(function(){
var w = window.innerWidth;
if(w >= 600){
$(".image").html("<img src='big-image.jpg'/>")
}
else{
$(".picture").html("<img src='small-image.jpg'/>")
}
});
There are other great solutions too (not supported in old browsers), take a look at image srcset <img srcset ... />
还有其他很棒的解决方案(旧浏览器不支持),看看 image srcset
<img srcset ... />
https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.