为什么将png的图片移动到另一个图层上面的时候,不显示图片,只是四周有虚线框啊,这个是怎么回事呢

如题所述

第1个回答  2011-11-29
半透明效果有时候会给页面增加不少色彩,特别是Vista盛行之后,半透明效果更加受推崇。在诸多可用于Web浏览的图片格式中,只有PNG格式和Gif格式可以实现半透明效果,不过Gif格式的半透明效果很有限,而且会大范围失真,所以目前最流行的方式就是使用PNG格式图片。不过可惜的是,在Internet Explorer 6及以下版本中都不支持PNG半透明效果(至少是不直接支持)。不过幸好Microsoft在这些浏览器中内置了其他的功能,可以帮助我们来实这种半透明的效果。

一、我们看一下普通情况下在现代浏览器中半透明效果的实现
首先要有一张半透明的PNG格式图片,这个制作很简单,在Photoshop中新建一个文件,在该文件中新建一个图层填充白色,然后调节透明度,删除背景,保存为PNG格式即可。你也可以 下载这个PNG文件使用。我们要做的工作很简单:只要把这个PNG图片指定为某个<div>的背景即可。例如可以使用下面的CSS规则:

view plaincopy to clipboardprint?
css">
body {
background:black url(bg.jpg) no-repeat 0 0;
text-align:center;
}
div {
width:80%;
margin:0 auto;
text-align:left;
padding:7px;
background-image:url(tran.png);
border:3px solid #fff;
}
body {background:black url(bg.jpg) no-repeat 0 0;text-align:center;} div {width:80%;margin:0 auto;text-align:left;padding:7px;background-image:url(tran.png);border:3px solid #fff;}
点击此处查看运行效果(只在IE7、Firefox等现代浏览器中有效)

二、在Internet Explorer 6中的实现
Internet Explorer中提供了提供了独有的滤镜效果,他通过filter: progid:DXImageTransform.Microsoft.AlphaImageLoader实现,现在是关于这个属性的有关知识:

引用来自 苏沈小雨CSS手册

enabled :  可选项。布尔值(Boolean)。设置或检索滤镜是否激活。
true | false true :  默认值。滤镜激活。
false :  滤镜被禁止。

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

特性:
Enabled :  可读写。布尔值(Boolean)。参阅 enabled 属性。
sizingMethod :  可读写。字符串(String)。参阅 sizingMethod 属性。
src :  可读写。字符串(String)。参阅 src 属性。

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

因此在Internet Explorer 6 中我们还要加上现在这段话

view plaincopy to clipboardprint?
* html div {
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src="tran.png");
background:none;
}
* html div {filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src="tran.png");background:none;}
注意:这里我们使用了 “*”的CSS hack,这个CSS Hack是Internet Explorer 6独有的,在Ineternet Explorer 6的DOM结构中,默认HTML的父节点为*,而在标准的DOM结构中HTML就是根节点。所以上面的CSS 规则只有Internet Explorer 6 认识。

点击此处查看运行效果

这样,我们在Internet Explorer 6、7、Firefox、Opera等最常用的浏览器的都实现了半透明效果了。

三、其它情况
但是上面都用PNG做背景的情况,有时候我们还会用PNG来做遮罩,比如下面这张Logo图片,

我们可以使用PNG遮罩来达到渐变效果:

那么我们可以使用下面的方法来实现
HTML代码段

view plaincopy to clipboardprint?

<div>
<img src="logo.jpg" alt="图片说明" />
<span></span>
</div>

<div> <img src="logo.jpg" alt="图片说明" /> <span></span></div>
注意:这种写法完全是为了迎合Internet Explorer 6,<div>容器用来帮助内部元素定位,<span>用来覆盖在<img>标签的上面达到半透明效果。下面我们只需要设定一下它们的位置就好了:

view plaincopy to clipboardprint?

div { position:relative;}
span {
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='filter.png');
float:left;
width:200px;
height:100px;
position:absolute;
top:0;
left:0;
}

div { position:relative;} span { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='filter.png'); float:left; width:200px; height:100px; position:absolute; top:0; left:0; }
不过,在Firefox中并没有filter这个属性,因此我们需要单独为Firefox中定制一个样式:

view plaincopy to clipboardprint?

div > span {
background:url(filter.png);
}

div > span { background:url(filter.png);}
这样我们就可以使用遮罩来实现半透明效果了:
点击查看运行效果

不过如果有太多的地方需要实现这样的遮罩效果的话,上面的处理方式还不是最好的,首先它有冗余的HTML标签,此外还使用了绝对定位和相对定位。如果我们把上面的代码进行封闭效果会更佳。这里我们可以使用下面这段JS代码进行封闭

view plaincopy to clipboardprint?

<!--[if lt IE 7]>
<script language="JavaScript">
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])
if ((version >= 5.5) && (document.body.filters))
{
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
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML
i = i-1
}
}
}
}
window.attachEvent("onload", correctPNG);
</script>
<![endif]-->

<!--[if lt IE 7]><script language="JavaScript">function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.{var arVersion = navigator.appVersion.split("MSIE")var version = parseFloat(arVersion[1])if ((version >= 5.5) && (document.body.filters)) {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;" + imgStyleif (img.align == "right") imgStyle = "float:right;" + imgStyleif (img.parentElement.href) imgStyle = "cursor:hand;" + imgStylevar strNewHTML = "<span " + imgID + imgClass + imgTitle+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" img.outerHTML = strNewHTMLi = i-1}}} }window.attachEvent("onload", correctPNG);</script><![endif]-->
这段代码只在Internet Explorer 6中才会被运行。
下面我们可以像在Internet Explorer 7 和Firefox中一样写代码:

view plaincopy to clipboardprint?

<div><img src="filter.png" alt="图片说明" /></div>

<div><img src="filter.png" alt="图片说明" /></div>
CSS

view plaincopy to clipboardprint?
div {
background:url(logo.jpg) no-repeat;
}

div { background:url(logo.jpg) no-repeat;}

解决了PNG跨浏览器的问题之后,我们可以利用它来实现更加复杂和更加绚丽的页面效果。
第2个回答  2013-07-30
将png格式图片做如下操作:图像——模式——RGB颜色。然后就可以拖动到另一个图片上了
第3个回答  2011-11-29
填充就OK啦追问

是移到一张红色背景的图片上,如果我新建一个白底的,就可以看到,但是移到这个红底的就看不到

追答

你改下图层样式试试

相似回答
大家正在搜