Home  >  Article  >  Web Front-end  >  IE6 compatible transparent background images and solutions_javascript skills

IE6 compatible transparent background images and solutions_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:44:101306browse

First, let me show you the renderings:

JS code:

<!--[if IE 6]>
<script src="~/Scripts/UI/DD_belatedPNG.js"></script>
<script>
$(function () {

//1. Through public class

DD_belatedPNG.fix(".pngFix,.pngFix:hover");

//2. Use selectors directly: class name, ID, label

DD_belatedPNG.fix(".imgpng,img");
});
</script>
<![endif]-->

html code:

DD_belatedPNG实现IE6下的透明背景

1、通过公共类pngFix

window.onload = function () { DD_belatedPNG.fix(".pngFix,.pngFix:hover"); }

2、直接用选择器:类名,ID,标签实现

DD_belatedPNG.fix(".imgpng,img");


css code:

<style>
.contain { width: 1000px; height: 300px; margin: 0 auto; background: #fff; }
.contain .con { width: 400px; float: left; }
.contain h1 { font-size: 18px; color: #333; margin-bottom: 10px; }
.contain h2 { font-size: 16px; color: #333; }
.imgpng { width: 200px; height: 150px; background: url(/Content/IMG/Ie6.png); }
</style>

The solution to the problem that transparent images in ie6 are not displayed transparently

Some pictures have browser compatibility. Pictures that are transparent are opaque in IE6, such as:

Effect in ie6

Normal display effect

For the above situation, you only need to add the following piece of code at the end of the code to solve the problem

<!--[if IE 6]>
<script type="text/javascript">
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) &#63; "id='" + img.id + "' " : ""
var imgClass = (img.className) &#63; "class='" + img.className + "' " : ""
var imgTitle = (img.title) &#63; "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
}
}
}
correctPNG();
</script>
<![endif]-->

IE6PNG transparent solution

1. Use filters Code:

 #pics
 {
  background:url(../images/Logo.png)no-repeat;
  /*以下为IE6设置PNG透明代码*/
  _background:none;
  _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/Logo.png");
 }

Tip: If you need to support linked hover, you need to define: cursor:pointer; in CSS to make it appear a hand shape, otherwise it will be the default mouse state.

Advantages:

1. Green without plug-ins;

2. High efficiency and fast speed;

3. When the network speed is slow, the background will not be gray and then transparent, and remote pictures are supported;

4. Supports pseudo-classes such as Hover, but two pictures must be used. If the network speed is slow, the second picture will not be displayed temporarily because it has not been fully loaded;

Disadvantages:

1. Tiling is not supported. Although the filter has sizingMethod="scale", stretching and scaling mode, the image will be deformed. If it is a pure color or a simple gradient color, it can be tiled horizontally;

2. The Img tag is not supported;

3. CSS Sprite is not supported;

Usage:

1. You can consider it when there is no img to introduce png;

2. You can consider it when there is no requirement for CSS Sprite;

3. You can consider it when there is no need for tiling;

2. Use JS to solve the gray background problem of img (png image inserted in the web page) png background in html

Just insert a piece of js into the page. The principle is the same as above, except that the img tag is replaced with the 45a2772a6b6107b401db3c9b82c049c2 tag, and the background of the 45a2772a6b6107b401db3c9b82c049c2 tag is set through a filter. It will do this for all inserted PNGs.

<!--[if IE 6]> 
<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) &#63; "id='" + img.id + "' " :"";
var imgClass = (img.className) &#63; "class='" + img.className + "'" : "";
var imgTitle = (img.title) &#63; "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]--> &#8203;

3. DD_belatedPNG.js file

1. Introduce the js file. Also, since this js is only useful when using IE6, in order to make our page execute more efficiently, we can modify the above code as follows. This JavaScript will only be called and executed when IE6 is used:

  692fc0b12e2f1333f193c301dc12affc4907d3dea7b54613e142a38a70acc1072cacc6d41bbb37262a98f745aa00fbf01b771f47d72d900ba74308aee59557f0

2. Call the function and set the parameters as follows:

Copy code The code is as follows:

DD_belatedPNG.fix("#pngImg,#pics,#picsRepeat");
            

  其中传入的参数为所使用png图片的标签的ID、类样式和标签名称,同样也可以按照下方这样来写

 

复制代码 代码如下:

DD_belatedPNG.fix("#contentimg");

              此方法则表示#content下的所有img标签透明

              如果为链接和链接的hover设置透明,那么您按照下方这么来写,在部分版本里面可以不用加入:hover直接写选择器即可,但是为了保险,建议咱们还是加上:hover:

 

复制代码 代码如下:

DD_belatedPNG.fix("#links,#link:hover");
             

写到这里并且您使用过jQuery或者CSSQuery类库,那么您一定熟悉上面的这种选择方法,总之就是,在CSS中您是如何选择的元素,那么在这个js函数(方法)中传入什么,只不过多个选择的时候,使用逗号隔开即可。

        小技巧:如果页面中存在很多png,DD_belatedPNG.fix();函数的参数岂不是很长?我们可以使用这种写法:

 

复制代码 代码如下:

DD_belatedPNG.fix(".pngFix,.pngFix:hover");

             如果使用上述的写法,我们的html中只需要在相对应的标签上加入class="pngFix"就行了,如果有多个类样式,按照平时的多个类样式的写法即可class="abc cbc pngFix",

             使用此方法的时候,我们每次都要加载两个js文件或者写两个3f1c4e4b6b16bbbd69b2ee476dc4f83a标签才行,这样不太好,http请求会增多,那么我们可以打开DD_belatedPNG.js文件,在尾部加入如下代码    

    即可:

 window.onload= function()
 {
  DD_belatedPNG.fix(".pngFix,.pngFix:hover");
 }

             这样我们只需要引入此JS,在需要透明的标签上加入class="pngFix"即可,简单 · 方便 · 快捷!

优点:

        1、CSS代码无需任何修改,按照平时的思路来写即可;

        2、无需配置;

        3、没有多余的gif图片;

        4、支持img;

        5、支持平铺;

        6、支持CSS Sprite;

        7、支持Hover等伪类;

缺点:

        1、额外加入了js文件(6.39k)和http请求,可以忽略不计;

        2、当文件载入之前,会先暂时呈现灰底;

        3、js文件过多的时候,可能会报错,导致js无法正常运行(这种情况极少出现,可以忽略不计);

以上就是本文讲述IE6兼容透明背景图片及解决方案的全部内容,希望对大家有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn