每个响应式网站开发教程都建议使用display:none CSS属性来隐藏移动浏览器加载的内容,这样网站加载得更快。这是真的吗?display:none不加载图像,还是在移动浏览器上加载内容?有没有什么方法可以防止在移动浏览器上加载不必要的内容?


当前回答

答案不像简单的是或不是那么简单。看看我最近做的一个测试的结果:

在Chrome浏览器:所有8个截图-*图像加载(img 1) 在Firefox中:仅加载当前正在显示的1个截图*图像(img 2)

所以在深入挖掘之后,我发现了这个,这解释了每个浏览器如何处理加载基于css显示的img资产:none;

摘自博客文章:

Chrome and Safari (WebKit): WebKit downloads the file every time except when a background is applied through a non-matching media-query. Firefox: Firefox won't download the image called with background image if the styles are hidden but they will still download assets from img tags. Opera: Like Firefox does, Opera won't load useless background-images. Internet Explorer: IE, like WebKit will download background-images even if they have display: none; Something odd appears with IE6 : Elements with a background-image and display: none set inline won't be downloaded... But they will be if those styles aren't applied inline.

其他回答

使用@media query CSS,基本上我们只是发布了一个项目,我们在桌面上有一个巨大的树的图像,但没有显示在桌面/移动屏幕上。所以防止图像加载非常简单

下面是一个小片段:

.tree {
    background: none top left no-repeat; 
}

@media only screen and (min-width: 1200px) {
    .tree {
        background: url(enormous-tree.png) top left no-repeat;
    }
}

你可以使用相同的CSS来显示和隐藏显示/可见/不透明度,但图像仍在加载,这是我们想出的最安全的代码。

嗨,伙计们,我也在与同样的问题作斗争,如何不在移动设备上加载图像。

但我想到了一个好办法。首先创建一个img标签,然后在src属性中加载一个空白的svg。现在,您可以将图像的URL设置为内联样式的content: URL('链接到您的图像');。现在用你选择的包装包装你的img标签。

<div class="test">
  <img src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/‌​svg%22/%3E" style="content:url('https://blog.prepscholar.com/hubfs/body_testinprogress.gif?t=1495225010554')">
</div>

  @media only screen and (max-width: 800px) {
      .test{
       display: none;
      }
    }

将包装器设置为在不希望加载图像的断点上不显示任何内容。img标记的内联css现在被忽略,因为包装器中没有显示的元素的样式将被忽略,因此图像不会被加载,直到到达包装器有显示块的断点。

好了,在移动断点上不加载img的简单方法:)

查看这个代码依赖,以获得一个工作示例:http://codepen.io/fennefoss/pen/jmXjvo

浏览器正变得越来越智能。现在你的浏览器(取决于版本)可能会跳过图像加载,如果它可以确定它是没有用的。

图像有一个显示:none样式,但它的大小可以由脚本读取。 Chrome v68.0不加载图像如果父隐藏。

你可以在那里查看:http://jsfiddle.net/tnk3j08s/

您也可以通过查看浏览器开发工具的“network”选项卡来检查它。

注意,如果浏览器是在一个小的CPU计算机上,不需要渲染图像(和布局页面)将使整个渲染操作更快,但我怀疑这在今天是否真的有意义。

如果你想阻止图像加载,你可以不添加IMG元素到你的文档(或设置IMG src属性为"data:"或"about:blank")。

** 2019回答**

在正常情况下显示:none不阻止图像被下载

/*will be downloaded*/

#element1 {
    display: none;
    background-image: url('https://picsum.photos/id/237/100');
}

但是如果一个祖先元素有display:none,那么后代的图像将不会被下载


/* Markup */

<div id="father">
    <div id="son"></div>
</div>


/* Styles */

#father {
    display: none;
}

/* #son will not be downloaded because the #father div has display:none; */

#son {
    background-image: url('https://picsum.photos/id/234/500');
}

其他阻止下载映像的情况:

1-目标元素不存在

/* never will be downloaded because the target element doesn't exist */

#element-dont-exist {
    background-image: url('https://picsum.photos/id/240/400');
}

2-两个相等的类加载不同的图像

/* The first image of #element2 will never be downloaded because the other #element2 class */

#element2 {
    background-image: url('https://picsum.photos/id/238/200');
}

/* The second image of #element2 will be downloaded */

#element2 {
    background-image: url('https://picsum.photos/id/239/300');
}

你可以在这里观看:https://codepen.io/juanmamenendez15/pen/dLQPmX

使用display:none with images的技巧是为它们分配一个id。这是不需要很多代码使其工作。下面是一个使用媒体查询和3个样式表的示例。一个用于手机,一个用于平板电脑,一个用于台式机。我有三张图片,手机,平板电脑和台式机的图片。在手机屏幕上只会显示手机的图像,平板电脑只会显示平板电脑的图像,桌面电脑则显示桌面电脑的图像。 下面是一个代码示例,使其工作:

源代码:

<div id="content">
<img id="phone" src="images/phone.png" />
<img id="tablet" src="images/tablet.png" />
<img id="desktop" src="images/desktop.png" />
</div>

不需要媒体查询的手机CSS。是img#手机让它工作:

img#phone {
    display: block;
    margin: 6em auto 0 auto;
    width: 70%;
    }

img#tablet {display: none;}
img#desktop {display: none;}

平板css:

@media only screen and (min-width: 641px) {
img#phone {display: none;}

img#tablet {
    display: block;
    margin: 6em auto 0 auto;
    width: 70%;
    }
}

和桌面css:

@media only screen and (min-width: 1141px) {
img#tablet {display: none;}

img#desktop {
    display: block;
    margin: 6em auto 0 auto;
    width: 80%;
    }
}

祝你好运,并让我知道它是如何为你工作的。