原生的pngFix(讓IE支援PNG透明)

來源:互聯網
上載者:User

由於幫人解決IE6 png不透明的bug,看到了對方使用jquery的pngfix,問題很快就找到了,是因為類庫衝突,也就是引入了兩個類庫導致的$衝突。

自己之前很少關注這方面東西,出於好奇看了一下源碼,發現實在不堪入目,就突發奇想採用原生的JS重構了一遍。如下:

/** * png fix by yansong at 2011-12-07 * * @example $.ready(function(){YS.pngFix();}); * @desc Fixes all PNG's in the document on document.ready * * @example $.ready(function(){YS.pngFix(document.getElementById('examples'));}); * @desc Fixes all PNG's within div with class examples * */// elem: document.getElementById('#example') || documentYS.pngFix = function(elem) {// IE 6if (document.uniqueID && !window.XMLHttpRequest) {if(elem !== undefined){if(elem.tagName == 'IMG'){fixImg(elem);return;}if(elem.tagName == 'INPUT'){fixInput(elem);return;}fixBg([elem]);}else{elem = document;}var reg1 = new RegExp().compile('url[\(][\'\"]?'),reg2 = new RegExp().compile('[\'\"]?[\)]');// fix imgfunction fixImg(elem){var imgs = elem.getElementsByTagName('img'),len = imgs.length,img,spanP,span,spanStyle,width,height;//console.log(len)while(len--){img = imgs[len];width = img.width || img.currentStyle.width;height = img.height || img.currentStyle.height;//console.log(1);if(img.src.indexOf('.png') !== -1){// parent nodespanP = document.createElement('span');spanStyle = spanP.style;spanStyle.position = 'relative';spanStyle.display = 'inline';spanStyle['float'] = img.currentStyle['float']||'none';spanStyle.border = img.currentStyle.border||'none';spanStyle.padding = img.currentStyle.padding||'0';spanStyle.margin = img.currentStyle.margin||'0';spanStyle.cursor = img.parentNode.currentStyle.cursor;// chidren nodespan = document.createElement('span');spanP.appendChild(span);spanStyle = span.style;spanStyle.cssText = 'position:relative;white-space:pre-line;display:inline;background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + img.src + '\', sizingMethod=\'crop\');';spanStyle.width = width;spanStyle.height = height;span.id = img.id;span.className = img.className;span.title = img.title;span.alt = img.alt;// do changeimg.parentNode.replaceChild(spanP, img);}}}// fix css background pngsfunction fixBg(elems){var len = elems.length, elem, bg;while(len--){elem = elems[len];bg = elem.currentStyle.backgroundImage;//console.log(bg);if(bg.indexOf('.png') != -1){elem.style.backgroundImage = 'none';// url('......') | url("...") | url(...)elem.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bg.replace(reg1, '').replace(reg2, '') + "',sizingMethod='crop')";}}}//fix input with png-sourcefunction fixInput(elem){var inputs = elem.getElementsByTagName('input'), len = inputs.length, input, src;while(len--){input = inputs[len];src = input.src;if(src&&src.indexOf('.png')!=-1){input.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + src + '\', sizingMethod=\'crop\');';input.src = 'blank.gif';}}}fixBg(elem.all);fixImg(elem);fixInput(elem);}};

其中yss遭遇到的最大問題就是在擷取background-image時的問題,通過elem.currentStyle.backgroundImage擷取到的值有三種情況:

1. url('http://www.test.com/1.png');

2. url("http://www.test.com/1.png");

3. url(http://www.test.com/1.png);

所以需要通過Regex去匹配並刪除,來擷取到:http://www.test.com/1.png

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.