简体   繁体   English

如何防止桌面版网站上的图片加载到移动设备上?

[英]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: srcsetsizes语法:

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" />


Browser support浏览器支持

For contemporary browser support for srcset and sizes attributes, see:对于当代浏览器支持srcsetsizes的属性,请参阅:


More Info (plus tutorial)更多信息(加上教程)

For more in-depth info and a tutorial on using srcset and sizes to build Responsive Images , see:有关使用srcsetsizes构建Responsive Images更深入信息和教程,请参阅:


Update in 2021 2021 年更新

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.效果也一样,而且更短、更干净、更易读。

CSS Media Queries and using background-images CSS 媒体查询和使用背景图像

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.至于个人偏好,我牢记移动优先的方法,首先声明移动样式,然后用媒体查询覆盖它们以获得更大的分辨率。

There are 4 ways that we can fix this issue:我们有 4 种方法可以解决此问题:

  1. Using picture tag.使用图片标签。
  2. Making the image a div with a background.使图像成为具有背景的 div。
  3. Lazy loading (native or with a plugin).延迟加载(本机或使用插件)。
  4. Content-visibility.内容可见性。

Details are in : link详情在: 链接

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.

相关问题 如何防止授权用户将我的网站嵌入移动应用程序 - How to prevent an authorized user from embedding my site in a mobile app 我的网站在显示正确内容之前会在桌面上显示移动视图一秒钟。 如何防止这样做? - My website shows the mobile view in the Desktop for a fraction of a second before showing the right content. How can I prevent it form doing so? 如何防止网站通过图像将 cookie 放入我的网站? - How can I prevent a site from putting cookies in my site through an image? 阻止移动用户下载桌面轮播图片 - Prevent mobile users from downloading desktop carousel images 如何通过我的 HTML 或 JavaScript 代码在移动设备上强制桌面视图 - How can I Force desktop view on a mobile device from my HTML or JavaScript code 如何防止我的登录模式在提交时刷新我的网站? - How can I prevent my login modal from refreshing my site on submit? 如果访问者拥有移动设备,则阻止从站点加载图像...有何看法? - Prevent load images from the site if the visitor has a mobile device… Opinions? 我如何从inappbrowser cordova请求一个桌面站点 - how can I request a desktop site from inappbrowser cordova 如何防止外部JS文件破坏我的网站? - How can I prevent an external JS file from breaking my site? 如果在移动设备上查看脚本,如何禁用或隐藏脚本在我的网站上运行? - How can I disable or hide a script from running on my site if being viewed on a mobile device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM