search
HomeWeb Front-endHTML TutorialWhen jquery changes the opacity of PNG under ie8, black edges appear, and the solution for png transparency under ie6_html/css_WEB-ITnose

At present, the Internet has higher and higher requirements for web page effects. It is inevitable to use PNG images. PNG is divided into several formats, PNG8 PNG24 PNG32, the most commonly used one is also the display effect and size comparison. The moderate one is PNG24, which supports translucency, transparency, and has very rich colors. However, because most of our Chinese people use the IE series or the browsers with IE as the core series, and because WINDOWS XP has a relatively large market share in the domestic market, and XP Many people on the Internet are still using IE6 IE7 IE8 and other browsers, and these browsers have more or less gaps in their PNG support. IE6 does not support PNG at all, and IE7 IE8 supports PNG incompletely. Under IE7 IE8, the image changes to transparent. When drawing, there will be a black border extending from the transparent area of ​​PNG. If there is translucency, the entire translucent area will be black. This is unacceptable for pages that require beautiful appearance. After some research, I found that Use PNG as the background and use Microsoft's unique filter to load the image, which can solve the problem of IE6 not supporting PNG, and can also solve the problem of black edges when using the JQUERY animation transparency effect under IE7 and IE8. The code has the real image, as follows:

<script>function correctPNG() {    var arVersion = navigator.appVersion.split("MSIE")    var version = parseFloat(arVersion[1])    if ((version >= 5.5) && (document.body.filters)) {        var lee_i = 0;        var docimgs=document.images;        for (var j = 0; j < docimgs.length; j++) {            var img = docimgs[j]            var imgName = img.src.toUpperCase();            if (imgName.substring(imgName.length - 3, imgName.length) == "PNG" && !img.getAttribute("usemap")) {                lee_i++;                var SpanID = img.id || 'ra_png_' + lee_i.toString();                var imgData = new Image();                imgData.proData = SpanID;                imgData.onload = function () {                    $("#" + this.proData).css("width", this.width + "px").css("height", this.height + "px");                }                imgData.src = img.src;                var imgID = "id='" + SpanID + "' ";                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;                j = j - 1;            }        }    }}//判断是否为IE8及以下浏览器,其实除了这三个浏览器不支持addEventListener,其它浏览器都没问题if (typeof window.addEventListener == "undefined" && typeof document.getElementsByClassName == "undefined") {    window.attachEvent("onload", correctPNG);}</script>

Reference the jquery1.8 class library before the end tag of /body of the page, and then add the above code. There will be no problem displaying PNG24 in IE6 7 8. If you need to execute When animate animation or obtaining pictures, it is found that PNG pictures cannot be found under IE 6 7 8, or there is no response when changing their positions and transparency. The reason is that correctPNG replaces the IMG tags of all PNGs on the page. SPAN tag, and then use filter: progid:DXImageTransform.Microsoft.AlphaImageLoader on the SPAN tag to load the PNG image. Therefore, the recommended approach is to include the image in a DIV. Only one IMG tag is allowed in this DIV, and then add the DIV Perform position or transparency related operations, for example:

<div id='test'><img  class="share-list-icon-shadow lazy"  src="/static/imghwm/default1.png"  data-src="style/images/icon-shadow.png" / alt="When jquery changes the opacity of PNG under ie8, black edges appear, and the solution for png transparency under ie6_html/css_WEB-ITnose" ></div><script>$("#test").animate({opacity:0.2,marginLeft:500},1000,function(){alert('run complete');});</script>

Another situation is that in addition to transparency and displacement, I also need to change the width and height of this image. For this situation, I recommend the following method:

<div id="test"><img  class="share-list-icon-shadow lazy" src="/static/imghwm/default1.png" data-src="style/images/icon-shadow.png" alt="When jquery changes the opacity of PNG under ie8, black edges appear, and the solution for png transparency under ie6_html/css_WEB-ITnose" ></div><script>$($("#test span")[0]||$("#test img")[0]).animate({opacity:0.2,marginLeft:500,width:'500px',height:'500px'},1000,function(){alert('run complete');});</script>

BUG: Under IE7 and IE8, if you dynamically modify the transparency of a png image, for example, if you apply a fadeIn, When the transparency of the image is adjusted to 25%, a very strange bug will appear. The transparent information of the png will be gone! It turned into a very ugly black color!

Solution to the bug that the background of png images turns black under IE7 and IE8:

1. Do not directly change the transparency of the image, but put a container for the image to modify the transparency of the container

For example, the original code is:

Modify it to: < ;div class="share-list-icon-shadow">When jquery changes the opacity of PNG under ie8, black edges appear, and the solution for png transparency under ie6_html/css_WEB-ITnose

2. Give this container Adding a background color

is very important. The key to solving bugs lies in this step, such as:

.share-list-icon-shadow{     width:60px;height:21px;     position:absolute;bottom:8px;left:0px;z-index: 1;     margin: 0 auto;     display:block;     background:#FAFDEF; } 

Under normal circumstances, the bug will be solved at this point. If you still have problems, please see below:

3. Add zoom: 1 to the container

What does zoom: 1 do? Why does IE have this bug?

These are two questions, but actually one answer. IE modifies transparency not through css attributes, but through filter , so if you want to understand this bug, you must find the reason from the filter. When filter acts on an object, the object must be tangible, that is, it must be a layout. IE has a very special attribute: hasLayout. This attribute can be given to the container as a layout. The attribute hasLayout is a bit weird. , you cannot start it by directly writing css, but must start it through javascript. In fact, there is another way to start it, which is to use a special css attribute to start hasLayout in disguise. This css attribute is zoom (other attributes For example, display:inline-block, float:left, etc. will also work, but only zoom has no side effects)

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
Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

What problems will be caused by using native select on your phone?What problems will be caused by using native select on your phone?Apr 30, 2025 pm 03:15 PM

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

What are the disadvantages of using native select on your phone?What are the disadvantages of using native select on your phone?Apr 30, 2025 pm 03:12 PM

What are the disadvantages of using native select on your phone? When developing applications on mobile devices, it is very important to choose the right UI components. Many developers...

How to optimize collision handling of third-person roaming in a room using Three.js and Octree?How to optimize collision handling of third-person roaming in a room using Three.js and Octree?Apr 30, 2025 pm 03:09 PM

Use Three.js and Octree to optimize collision handling of third-person roaming in the room. Use Octree in Three.js to implement third-person roaming in the room and add collisions...

What problems will you encounter when using native select on your phone?What problems will you encounter when using native select on your phone?Apr 30, 2025 pm 03:06 PM

Issues with native select on mobile phones When developing applications on mobile devices, we often encounter scenarios where users need to make choices. Although native sel...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool