【CSS】修复IE6下不缓存背景图的BUG

来源:互联网 发布:移动网络信号测试软件 编辑:程序博客网 时间:2024/06/05 13:34

  可能有些朋友不知道这个BUG,IE6浏览器默认情况下是不缓存背景图片的,CSS每次更换背景图片位置的时候都会发起重新加载的请求,每次重新发起请求图片就会产生极其明显的闪烁,这样对用户的体验效果不是好,IE6下的背景图片每次使用都会重新发送请求(not 本地),连一个hover效果时候同样的背景图片仅仅位置不同而已,ie6也会再次发送请求,这个令人崩溃的事情需要解决掉:对于ie来说,filter:expression 很强大,能够实现的功能超级多,但是对于执行效率如生命的程序员来说,它的效率不敢令人恭维,使用CSS解决此BUG并不是一种完美的解决方案。
  
  使用CSS解决IE6背景图不缓存的问题,下面的这种方法不推荐使用,因为CSS的expression对网页的加载速度有一定影响,所以避免使用,该代码针对IE7以下的IE版本有效。
  
filter:expression(document.execCommand(“BackgroundImageCache”,false,true));
或者在CSS的filter上做下改动,让它只在IE6下被识别
_filter:expression(document.execCommand(“BackgroundImageCache”,false,true));
推荐使用JS解决IE6背景图不缓存的问题(JS方法本人没有亲测过,如无效勿喷)

<!--[if IE 6]><script type="text/javascript">//<![CDATA[try {  document.execCommand("BackgroundImageCache", false, true);} catch(e) {}//]]></script><![endif]-->

JS二

(function(){    try{        var userAgent = navigator.userAgent.toLowerCase();        var env = null;        var ver = 0;        env = userAgent.match(/msie ([\d.]+)/);ver = env ? parseInt(env[1], 10) : 0;        if(ver == 6){            try{                 document.execCommand("BackgroundImageCache", false, true);            }catch(e){}        }    }catch(e){}})();

JS三

(function(){    var browser=new Object();    browser.name=navigator.appName;     if(browser.name.indexOf("Microsoft")!=-1){         browser.version=navigator.appVersion.indexOf("MISE");        browser.version=parseInt(navigator.appVersion.substring(browser.version+4));        if(browser.version<=6){ document.execCommand("BackgroundImageCache",false,true); }    }})(window.navigator);