PNG images are compatible with IE and FF solutions-js Control

Source: Internet
Author: User
PNG format image files are not very compatible with browsers, but such transparent PNG files can also be used as virtual edges, and are becoming more and more popular in design. Especially in IE6, there are many bugs and poor support, but there is no way to have a large number of users. We should first consider the support for it. FF supports PNG images, and IE7 also supports PNG images.
There is a simple way to solve the PNG compatibility problem of IE6 and FF on the Internet, and a piece of js code can be done. The Code is as follows. We recommend that you save this code as an independent JS file for convenient page calling. 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.css text
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
}
}
}
Function alphabackgrounds (){
VaR rslt = navigator. appversion. Match (/MSIE (D +. d + )/,'');
VaR itsallgood = (rslt! = NULL & number (rslt [1]) >=5.5 );
For (I = 0; I <document. All. length; I ++ ){
VaR BG = Document. All [I]. currentstyle. backgroundimage;
If (BG ){
If (BG. Match (/. PNG/I )! = NULL ){
VaR mypng = BG. substring (5, BG. Length-2 );
// Alert (mypng );
Document. All [I]. style. Filter = "progid: DXImageTransform. Microsoft. alphaimageloader (src = '" + mypng + "', sizingmethod = 'crop ')";
Document. All [I]. style. backgroundimage = "URL ('')";
// Alert (document. All [I]. style. Filter );
}
}
}
}

If (navigator. Platform = "Win32" & navigator. appname = "Microsoft Internet Explorer" & window. attachevent ){
Window. attachevent ("onLoad", correctpng );
Window. attachevent ("onLoad", alphabackgrounds );
}

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.