您的位置:首页 > Web前端 > JQuery

JQuery解决IE6不支持透明png图片、png背景

2013-09-05 20:25 736 查看
ie6不支持png背景是常发生的事情了,现在我们直接利用jquery fixpng插件来解决这个问题,相对js来说简单了很多,有需要的朋友可参考。1,解决直接以 img 标签显示的png图片

$(document.body).fixpng({scope:’img’});
可将body内的所有png图片显示为透明效果.
2,解决以 透明png为背景 图片的情况
$(‘div’).fixpng();
可将所有 div 标签 背景图为png的变为透明
演示
<script type="text/javascript" src="jquery-latest.pack.js"></script>
<script type="text/javascript" src="jquery.pngFix.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
  $(document).pngFix();
});
</script>

把下面代码保存成jquery.pngFix.js文件,然后导入调用
(function($){var jspath=$('script').last().attr('src');var basepath='';if(jspath.indexOf('/')!=-1){basepath+=jspath.substr(0,jspath.lastIndexOf('/')+1);}$.fn.fixpng=function(options){function _fix_img_png(el,emptyGIF){var images=$('img[src*="png"]',el||document),png;images.each(function(){png=this.src;width=this.width;height=this.height;this.src=emptyGIF;this.width=width;this.height=height;this.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+png+"',sizingMethod='scale')";});}function _fix_bg_png(el){var bg=$(el).css('background-image');if(/url(['"]?(.+.png)['"]?)/.test(bg)){var src=RegExp.$1;$(el).css('background-image','none');$(el).css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"',sizingMethod='scale')");}}if($.browser.msie&&$.browser.version<7){return this.each(function(){var opts={scope:'',emptyGif:basepath+'blank.gif'};$.extend(opts,options);switch(opts.scope){case'img':_fix_img_png(this,opts.emptyGif);break;case'all':_fix_img_png(this,opts.emptyGif);_fix_bg_png(this);break;default:_fix_bg_png(this);break;}});}}})(jQuery);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: