PNG图片打造半透明背景

   你是否经常换皮肤,各种皮肤背景色彩不一样,使上传的图片有各种各样的杂边而影响美观。
  如果你用png半透明图片就不会有这种现像了。
  PNG图片在网页设计中扮演着一个很重要的角色,利用PNG图片的特点可以制作出实用,绚丽的效果,可是对于PNG图片的支持却不是很理想,Firefox和Opera对PNG支持的比较好,特别是Firefox浏览器。可是IE却不理PNG,使得设计者无法很随意的使用png图片。

IE5.5+的AlphaImageLoader滤镜为通向png提供了一个道路,如果他载入的是PNG(Portable Network Graphics)格式,则0%-100%的透明度也被提供。

(X)HTML:
程序代码 程序代码
<div id="wrap">
<p><a href="http://d.thec.cn/soking"><strong>PNG半透明背景图片效果</strong></a><br />:PNG图片在网页设计中扮演着一个很重要的角色,利用PNG图片的特点可以制作出需要使用绚丽的效果,可是对于PNG图片的支持却不是很理想,Firefox和Opera对PNG支持的比较好,特别是Firefox浏览器。可是IE却不理PNG,是的设计者无法很随意的使用png图片。</p>
<p>IE5.5+的AlphaImageLoader滤镜为通向png提供了一个道路,如果他载入的是PNG(Portable Network Graphics)格式,则0%-100%的透明度也被提供。我们就利用这个滤镜和hack来设计一个半透明png背景图片的模型
</p>
<p><a class="test" href="http://d.thec.cn/soking">这里是个超链接测试</a></p>
</div>


CSS:
程序代码 程序代码
#wrap{ width:460px;margin:20px auto 20px 80px;text-align:left;height:300px; padding:20px; border:1px #eee solid;}
/*not for ie 6.0*/
html>body #wrap{background: url(img/bgcanvas.png) repeat;}
/*for ie 6.0*/
* html #wrap {
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src="img/bgcanvas.png");
background:none;
}
#wrap a{color:#c00; text-decoration: none; position:relative;}/*解决IE下链接失效的问题*/
#wrap a:hover{ text-decoration:underline;}


效果预览:http://d.thec.cn/soking/files/200608/pngBg.htm

经测试,兼容IE6.0 Firefox 1.5 Opera 8.5,因为AlphaImageLoader滤镜不支持IE5.0,所以IE5.0没有达到想要的效果,基本IE5.0与PNG无缘了。

Quotes From CSS2.0手册
程序代码 程序代码
语法:
filter : progid:DXImageTransform.Microsoft.AlphaImageLoader ( enabled=bEnabled , sizingMethod=sSize , src=sURL )
属性:
enabled : 可选项。布尔值(Boolean)。设置或检索滤镜是否激活。true | false
      true : 默认值。滤镜激活。
      false : 滤镜被禁止。

sizingMethod : 可选项。字符串(String)。设置或检索滤镜作用的对象的图片在对象容器边界内的显示方式。
                crop : 剪切图片以适应对象尺寸。
        image : 默认值。增大或减小对象的尺寸边界以适应图片的尺寸。
        scale : 缩放图片以适应对象的尺寸边界。
        src : 必选项。字符串(String)。使用绝对或相对 url 地址指定背景图像。假如忽略此参数,滤镜将不会作用。

说明:
在对象容器边界内,在对象的背景和内容之间显示一张图片。并提供对此图片的剪切和改变尺寸的操作。如果载入的是PNG(Portable Network Graphics)格式,则0%-100%的透明度也被提供。
PNG(Portable Network Graphics)格式的图片的透明度不妨碍你选择文本。也就是说,你可以选择显示在PNG(Portable Network Graphics)格式的图片完全透明区域后面的内容。





如果想在pjblog里使用半透明png图片,使用以下方法
打开common/common.js 在最后面插件如下代码就可以了
程序代码 程序代码
//  Dynamic PNG fix for IE 5 & 6 -------------------------------------------------
//  Credit:  Bob Osola - http://homepage.ntlworld.com/bobosola/
var strGif = "transparent.gif";
var strFilter = "progid:DXImageTransform.Microsoft.AlphaImageLoader";  
function correctPNG()  {  
     for(var i=0; i<document.images.length; i++)  {
         var img = document.images[i];  
         var imgName = img.src.toUpperCase();  
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")  {
             var imgID = (img.id) ? "id='" + img.id + "' " : "";  
             var imgClass = (img.className) ? "class='" + img.className + "' " : "";  
             var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";  
             var imgStyle = "display:inline-block;" + img.style.cssText;  
             if (img.align == "left") { imgStyle = "float:left;" + imgStyle; }  
             if (img.align == "right") { imgStyle = "float:right;" + imgStyle; }  
             if (img.parentElement.href) { imgStyle = "cursor:hand;" + imgStyle; }  
             if (img.useMap)  {  strAddMap = "<img style=\"position:relative; left:-" + img.width + "px;"  + "height:" + img.height + "px;width:" + img.width +"\" " + "src=\"" + strGif + "\" usemap=\"" + img.useMap + "\" border=\"" + img.border + "\">";  }  
             var strNewHTML = "<span " + imgID + imgClass + imgTitle  + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"  + "filter:" + strFilter  + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>";  
             if (img.useMap) strNewHTML += strAddMap;  img.outerHTML = strNewHTML;  i = i-1;  }  }  
             for(i=0; i < document.forms.length; i++)  {  findImgInputs(document.forms(i));  } }  
             function findImgInputs(oParent) {  
                 var oChildren = oParent.children;  
                 if (oChildren);  {  
                     for (var i=0; i < oChildren.length; i++ )  {  
                         var oChild = oChildren(i);  
                         if ((oChild.type == 'image') && (oChild.src))  {  
                             var origSrc = oChild.src;  oChild.src = strGif;  oChild.style.filter = strFilter + "(src='" + origSrc + "')";  }  findImgInputs(oChild);  }  } } window.attachEvent("onload", correctPNG);


【制作png的图片的时候背景色要设为透明。导出时必须导成PNG32】
下面的png图片是效果
<img src=http://www.dnxh.cn/blog/attachments/month_0701/j2007131121051.png>


效果网站:http://www.dnxh.cn/blog/article.asp?id=312 (白色背景)
     本页黑色背景


[本日志由 ilank 于 2007-06-03 09:56 PM 编辑]
文章来自: 收藏
引用通告: 查看所有引用 | 我要引用此文章
Tags: PNG半透明
相关文章:

评论: 1 | 引用: 0 | 查看次数: 40808
  • 1
yy [2007-02-09 09:42 PM]
恩,这个很好哦~~~
  • 1
发表评论
昵 称:
密 码: 游客发言不需要密码.
验证码: 验证码
内 容:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 关闭 | [img]标签 关闭