Remove image shadows in PNG format by JS

Source: Internet
Author: User

Borrow a sentence:

PNG images have good quality. The shadow effect does not have complex edges and is smooth. If you insert a webpage, you can add a lot of colors to the website content! More importantly, the quality of images on the page is improved without increasing the image size. It is very convenient to insert images with irregular borders on over-colored backgrounds with complex backgrounds!

However, at present, the .png image inserted in the transparent background cannot be displayed normally in IE. IE will automatically add a gray background to the image in ". PNG" format.

To address the above problems, IE browser provides filter CSS, which can be used to control the background color. Transparent, fuzzy, and so on.

Based on this idea, you can set the CSS directly and set the CSS path using Js.

Original Style (if the corresponding PNG image is not found, you can try another one at Will)

 

1. Define CSS directly

<spanstyle="width:512px;height:512px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://online.sccnn.com/icon/1130/pin_001.png',sizingMethod='scale');"></span>

Width and height must be defined here, otherwise they cannot be displayed.

2. Set CSS using JS

<script>function fixPNG(myImage) {var arVersion = navigator.appVersion.split("MSIE");var version = parseFloat(arVersion[1]);if ((version >= 5.5) && (version < 7) && (document.body.filters)){     var imgID = (myImage.id) ? "id='" + myImage.id + "' " : "";     var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : "";     var imgTitle = (myImage.title) ? "title='" + myImage.title   + "' " : "title='" + myImage.alt + "' ";     var imgStyle = "display:inline-block;" + myImage.style.cssText;     var strNewHTML = "<span " + imgID + imgClass + imgTitle     + " style=\"" + "width:" + myImage.width     + "px; height:" + myImage.height     + "px;" + imgStyle + ";"     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"     + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>";     myImage.outerHTML = strNewHTML;}} </script>

The complete HTML is as follows:

<HTML><HEAD><TITLE> Png Image </TITLE><script>function fixPNG(myImage) {var arVersion = navigator.appVersion.split("MSIE");var version = parseFloat(arVersion[1]);if ((version >= 5.5) && (version < 7) && (document.body.filters)){     var imgID = (myImage.id) ? "id='" + myImage.id + "' " : "";     var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : "";     var imgTitle = (myImage.title) ? "title='" + myImage.title   + "' " : "title='" + myImage.alt + "' ";     var imgStyle = "display:inline-block;" + myImage.style.cssText;     var strNewHTML = "<span " + imgID + imgClass + imgTitle     + " style=\"" + "width:" + myImage.width     + "px; height:" + myImage.height     + "px;" + imgStyle + ";"     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"     + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>";     myImage.outerHTML = strNewHTML;}} </script></HEAD><BODY><spanstyle="width:512px;height:512px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://online.sccnn.com/icon/1130/pin_001.png',sizingMethod='scale');"></span></BODY></HTML>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.