惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
I
InfoQ
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
博客园_首页
The Cloudflare Blog
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
罗磊的独立博客
月光博客
月光博客
V
V2EX
大猫的无限游戏
大猫的无限游戏
腾讯CDC
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
B
Blog RSS Feed
Webroot Blog
Webroot Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
C
CERT Recently Published Vulnerability Notes
人人都是产品经理
人人都是产品经理
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recent Announcements
Recent Announcements
Cyberwarzone
Cyberwarzone
G
Google Developers Blog
H
Heimdal Security Blog
MyScale Blog
MyScale Blog
The Register - Security
The Register - Security
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
T
Tenable Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
O
OpenAI News
C
Check Point Blog
Forbes - Security
Forbes - Security
SecWiki News
SecWiki News
K
Kaspersky official blog
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Full Disclosure
阮一峰的网络日志
阮一峰的网络日志

博客园 - sin

2017年6月做的一个梦 开始写诗了 一张判断你是用左脑还是右脑的图 Javascript 刷新框架及页面的方法总集 - sin - 博客园 SQL高级查询技巧 自己写的SQL存储过程分页方法 .net简单添加数据库方法 ASP.NET刷新页面的六种方法 DataGrid传统分页方式 - sin - 博客园 ACCESS"数据库提示它已经被别的用户以独占方式打开,或没有查看数据的权限"的问题 - sin - 博客园 div长度相等的几个办法 在IE浏览器中的奇怪页面表现 HTML标签大全 css杂问题 用css模拟title和alt的提示效果 用Web标准进行开发 div+css 面试题目 有什么有效方法清除多余的CSS代码 95%的中国网站需要重写CSS-写在px和em的区别下
png图片在ie不透明的解决方案和浏览器通用的透明效果
sin · 2007-06-22 · via 博客园 - sin

PNG(Portable Network Graphics)是W3C推荐的网页图片通用格式,但是Microsoft的IE6以下(IE7已经支持)没有把PNG的Alpha 通道打开,造成透明PNG图片的效果出不来。在Firefox、Opera下显示正常的透明PNG图片,在IE下浏览时就会带有灰色背景色,不透明的就像下图示例:

解决方法之一是给透明PNG图片加一个滤镜:

引用内容 引用内容

<div style="width:100%;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='透明PNG图片路径',sizingMethod='image')></div>

或是用CSS的方式控制,把style写在CSS文件中,就不用每贴一张图都要写这么长的代码了。

透明的效果就像这样:

HTML代码



[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

第二种方法就是用javascript来控制,程序代码如下:

引用内容 引用内容

<script>
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
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>

在页面中加入此代码PNG图片即可透明。。。

浏览器通用透明效果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>runcode</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Sheneyan" />
<style type="text/css">
body{background:gray;}
div#test{
  background:#000;
  color:white;
  width:200px;
  position:absolute;
  left:10px;
  top:10px;
  filter: Alpha(opacity=50);
  -moz-opacity:.50;
  opacity:0.5;
}
</style>

</head>
<body>
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
这里是一堆文字背景~<br />
<div id="test">我应该是半透明的……</div>
</body>
</html>