博客 - 标签:ie6

网页中图片和背景的透明问题(解决IE6 PNG 透明)

2009-04-11 下午 - html/css/xml/xsl - - -

  昨晚帮一朋友制作一个页面,看似很简单!却花了我4个小时。主要是花在页面里3张png的图片上,似了好几种方法,弄来弄去的,头都大了!今天来整理一下,方法都来自互联网,出处很难说明了

!做个笔录吧!

一.IE6使用gif,其他则使用png

.pngImg { background:url(image.png); _background:url(image.gif);}

  对于页面来说,该方法是最为完美的,缺陷即gif图像的效果问题。该方法用到的时候很少,依图片而定。

二.滤镜filter

background:url(a.png) repeat-x 0 0;
_
background:none;
_
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="a.png" ,sizingMethod="crop");

  利用滤镜filter让IE6,并利用css hack区分开IE6,实际应用中使用条件注释将更完美。

  缺陷:IE6下背景无法平铺,这个问题很严重。同时在性能上也有小问题,页面中次数不是很多的时候该办法还是可行的。

三.html中的img(插入在网页中的png图像)

function correctPNG()
{
for(var i=0; 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 = " + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+
"(src=\'" + img.src + "\', sizingMethod='scale');\">"
img.outerHTML = strNewHTML
i = i-1
}
}
}
window.attachEvent("onload", correctPNG);

  页面中插入一段js即可。原理同上,只是将img放入了background。

四.调用iepngfix.htc

方法来源:http://www.twinhelix.com/

下载htc文件:http://www.twinhelix.com/css/iepngfix/iepngfix.zip

以下片段添加至css文件

.pngImg {behavior: url(iepngfix.htc);}

以下片段添加至html文件

<div class="pngImg">PNG背景图片</div>
<img src="png图片" class="pngImg" alt="">

详细的应用方法这里就不介绍啦。

在逼不得已且身不由己必须使用PNG的情况下,这种方法应该是比较优秀的,虽然不能完美的解决IE6的平铺,但是至少是实现了拉伸,使得很多情况下可以代替平铺来使用。当然效率的问题任然是存在

滴,这是木有办法的。一般的小企业站用用是没“罪过”的!因为我昨晚就用在了一个实际的案例中….实在没辙了,客户一定要那种高质感的透明,煞费苦心的说了半天,最终只有妥协…

五.让“块”透明的方法

.div { FILTER: alpha(opacity=20); moz-opacity: 0.2; opacity: 0.2;}

测试IE6,IE7,IE8,FF2,FF3均通过。提示:IE6,IE7需设置一个宽度(100%也行),否则看不到效果。

4 Comments »

ie6玩转position:fixed(实现固定定位)

2008-07-4 下午 - html/css/xml/xsl - - - -

  原理:出去网页以及原有的滚动条,用一个[div]模拟它。然后将要固定定位的区块置于这个模拟层之外,即可实现需要的效果。因为这时滚动条拖动的只是模拟的那个层,而固定定位的这个层根本就不在这个区块内,所以就不会跟随滚动条而动。

  利用CSS控制行的属性:

下载: code1.html
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2. <style type="text/css"> 
  3. html,body { margin:0; padding:0; overflow:hidden;height:100%; width:100%; text-align:left;}
  4. .body { position:relative; width:100%; height:100%; overflow-y:scroll; overflow-x:auto; cursor:default;}
  5. .box {  position:absolute; z-index:999; margin:0 auto; bottom:10px; left:40px; width:800px; height:40px; border:1px solid #369; background:#f7f7f7; line-height:40px;}
  6. </style> 
  7. <div class="box">我是不会被你拖着走的!</div> 
  8. <div class="body"> 
  9.  <p style="margin:15px;">看屏幕底端的区块,再拖动浏览器的滚动条,结果怎么样?满意吧,惊喜吧!</p> 
  10.  <div style="height:1000px;"></div> 
  11. </div>

  如果不明白,就自己再多想想吧!

  还有种方法就是利用js来实现了。在此也顺便贴出来吧!

下载: code2.html
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2. .floatBar { width:100%; position:fixed; bottom:0!important;height:31px; background:#f7f7f7; border:1px solid #369;}
  3. .floatBar {
  4. _position:absolute;
  5. _top:expression(eval(document.compatMode &&
  6.         document.compatMode=='CSS1Compat') ?
  7.         documentElement.scrollTop
  8.         +(documentElement.clientHeight-this.clientHeight) - 100
  9.         : document.body.scrollTop
  10.         +(document.body.clientHeight-this.clientHeight) - 100);
  11. }
  12.  
  13. <div style="height:800px;">下面的层是不随滚动条而动的。</div>
  14. <div class="floatBar">
  15. 这个是利用js实现的固定定位。
  16. </div>

  两个方法都不错哦!值得推荐的!

1 Comment »