请在主流浏览器中打开测试页面,在fiddler里查看http请求.
' S- U% k; C6 G% R* F1. 隐藏图片
: b( ]# C7 u; \- Y6 b" J<img src=1.jpg style=display: none />测试:test_1.html
; h* }' g4 q. z; h结论:只有opera不产生请求.1 [, p% ^2 E( G" X9 Y
注意:用visibility: hidden隐藏图片时,在opera下也会产生请求.
) }# [, _( O- T& W2. 重复图片% w2 y& {8 h9 D" g8 T, h3 v
<img src=1.jpg /><img src=1.jpg />测试:test_2.html/ j W( L: |6 H5 B
结论:所有浏览器都只产生一次请求 .
& o8 n! g6 v3 I2 B* P$ s9 w6 q3. 重复背景
9 a, v. [# j4 J2 C! U! d<style type=text/css> .test1 { background: url(1.jpg) } .test2 { background: url(1.jpg) }</style><div class=test1>test1</div><div class=test2>test2</div>测试:test_3.html9 }( a2 |$ s6 F1 A6 Q' ?
结论:所有浏览器都只产生一次请求.* B. F, c: B3 o3 M/ \7 G
4. 不存在地元素地背景
' W a7 H9 U0 l# V3 @$ b9 `<style type=text/css> .test1 { background: url(1.jpg) } .test2 { background: url(2.jpg) } /* 页面中没有class为test2地元素 */</style><div class=test1>test1</div>测试:test_4.html
# u3 p3 P4 Q4 L4 F, @; \5 I1 F结论:背景仅在应用地元素在页面中存在时,才会产生请求.这对css框架来说,很有意义.
( k) d7 J3 `; G( y! |5. 隐藏元素地背景2 G3 [+ ~& P* m9 z/ P3 h5 z4 \
<style type=text/css> .test1 { background: url(1.jpg); display: none; } .test2 { background: url(2.jpg); visibility: hidden; }</style><div class=test1>test1</div>测试:test_5.html8 q0 l8 i+ u4 I% ?5 i" Q8 `
结论:opera和firefox对于用display: none隐藏地元素背景,不会产生http请求.仅当这些元素非display: none时,才会请求背景图片.
# W- O, n7 O8 ]7 ]6. 多重背景7 s5 V5 W. }1 `
<style type=text/css> .test1 { background: url(1.jpg); } .test1 { background: url(2.jpg); }</style><div class=test1>test1</div>测试:test_6.html
* E& d. U# `3 T# P" V* W结论:除了基于webkit渲染引擎地safari和chrome,其它浏览器只会请求一张背景图.
7 R8 a- W! W3 {8 m注意:webkit引擎浏览器对背景图都请求,是因为支持css3中地多背景图.% i+ ~! I$ W# p6 g7 a t5 b
7. hover地背景加载' F' _: o3 q2 _6 y+ B8 }
<style type=text/css> a.test1 { background: url(1.jpg); } a.test1:hover { background: url(2.jpg); }</style><a href=# class=test1>test1</a>测试:test_7.html
2 b) s" u4 J Q& m: U( b结论:触发hover时,才会请求hover状态下地背景.这会造成闪烁,因此经常放在同一张背景图里通过翻转来实现.7 o! x- a: \" T6 d
注意:在图片no-cache地情况下,ie每次hover状态改变时,都会产生一次新请求.很糟糕.! l1 [0 l& i) V8 z
2009-05-13晚补充:上面地解释有误,更详细地解释请参考续篇.翻转技巧指地是sprite技术,例子:test_7b.html, 在ie6下不会产生闪烁.
3 F1 F; Z/ V' z) ~+ g" m# ?5 E$ d8. js里innerhtml中地图片
7 J1 E+ {4 U; V6 J. _<script type=text/javascript> var el = document.createelement('div'); el.innerhtml = '<img src=1.jpg />'; //document.body.appendchild(el);</script>测试:test_8.html
6 A1 z3 Y& @* i* j- q. p结论:只有opera不会马上请求图片.
+ c- ~. U# f7 a+ x2 y注意:当添加到dom树上时,opera才会发送请求.
- Q/ r- R! B J) v; q- }6 s6 j+ f; |! z
更多网页制作信息请查看: 网页制作 |