分享

图片的HTTP请求总结,web图片http请求汇总,网页图片预加载

 龚神 2018-05-27

图片的http请求,有很多种情况,那么究竟什么情况下面不会发生请求呢?下面我用案例一一列举一下,希望对你深入了解http图片请求有所帮助。(请求图片截图,均已谷歌浏览器为例!文章转载请联系我,或者注明haorooms博客并附链接地址!)

1. 隐藏图片

<> src='haorooms.jpg' style='display: none' />

http请求如下:

enter image description here

结论:只有Opera不产生请求。注意:用visibility: hidden隐藏图片时,在Opera下也会产生请求。

2. 重复图片

<> src='haorooms.jpg' /><> src='haorooms.jpg' />

http请求如下:

enter image description here

结论:所有浏览器都只产生一次请求 。

3. 重复背景

<> type='text/css'> .test1 { background: url(haorooms.jpg) } .test2 { background: url(haorooms.jpg) }<> class='test1'>test1
<> class='test2'>test2

http请求如下:

enter image description here

结论:所有浏览器都只产生一次请求。

4. 不存在的元素的背景

<> type='text/css'> .test1 { background: url(haorooms.jpg) } .test2 { background: url(http2.jpg) } /* 页面中没有class为test2的元素 */<> class='test1'>test1

http请求如下:

enter image description here

结论:背景仅在应用的元素在页面中存在时,才会产生请求。这对CSS框架来说,很有意义。

5. 隐藏元素的背景

<> type='text/css'> .test1 { background: url(haorooms.jpg); display: none; } .test2 { background: url(http2.jpg); visibility: hidden; }<> class='test1'>test1

http请求如下:

enter image description here

结论:Opera和Firefox对于用display: none隐藏的元素背景,不会产生HTTP请求。仅当这些元素非display: none时,才会请求背景图片。

6. 多重背景

<> type='text/css'> .test1 { background: url(haorooms.jpg); } .test1 { background: url(http2.jpg); }<> class='test1'>test1

上面这段代码的http请求,只会请求http2.jpg这一张图片,原因是test1的class把上面的给覆盖掉了,所有只请求后面的一张图片!

假如用css3多张背景图片的写法:

<> type='text/css'> .test1 { background-image:url('haorooms.jpg'),url('http2.jpg'); }<> class='test1'>test1

那么http请求如下:

enter image description here

webkit引擎浏览器对背景图都请求,是因为支持CSS3中的多背景图。关于css3多张背景图片,我前面写了一篇文章介绍,请看:http://www./post/css3_bg_multi

7. hover的背景加载

<> type='text/css'> a.test1 { background: url(haorooms.jpg); } a.test1:hover { background: url(http2.jpg); }<> href='#' class='test1'>test1

http请求如下:

enter image description here

结论:触发hover时,才会请求hover状态下的背景。不触发的话,只请求默认的背景图片。

8. JS里innerHTML中的图片

在无JS支持的环境下,可以采用隐藏元素来预加载:

<> src='haoroomscom.jpg' style='visibility: hidden; height: 0; width: 0' />

总结

1、对于隐藏图片和隐藏元素的背景,Opera不会产生请求。

2、对于隐藏元素的背景,Firefox也不会产生请求。

3、对于尚未插入DOM树的img元素,Opera不会产生请求。

4、基于webkit引擎的Safari和Chrome,支持多背景图。

5、其它情景,所有主流浏览器保持一致。

希望上面的图片http请求对您有所帮助,大家可以相互留言交流!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多