2 * jQuery ifixpng plugin
3 * (previously known as pngfix)
4 * Version 2.1 (23/04/2008)
5 * @requires jQuery v1.1.3 or above
7 * Examples at: http://jquery.khurshid.com
8 * Copyright (c) 2007 Kush M.
9 * Dual licensed under the MIT and GPL licenses:
10 * http://www.opensource.org/licenses/mit-license.php
11 * http://www.gnu.org/licenses/gpl.html
18 * optional if location of pixel.gif if different to default which is images/pixel.gif
19 * $.ifixpng('media/pixel.gif');
21 * $('img[src$=.png], #panel').ifixpng();
23 * @apply hack to all png images and #panel which icluded png img in its css
29 * @author jQuery Community
35 * helper variables and function
37 $.ifixpng = function(customPixel
) {
38 $.ifixpng
.pixel
= customPixel
;
41 $.ifixpng
.getPixel = function() {
42 return $.ifixpng
.pixel
|| 'images/pixel.gif';
46 ltie7
: $.browser
.msie
&& $.browser
.version
< 7,
47 filter : function(src
) {
48 return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src
+"')";
53 * Applies ie png hack to selected dom elements
55 * $('img[src$=.png]').ifixpng();
56 * @desc apply hack to all images with png extensions
58 * $('#panel, img[src$=.png]').ifixpng();
59 * @desc apply hack to element #panel and all images with png extensions
64 $.fn
.ifixpng
= hack
.ltie7
? function() {
65 return this.each(function() {
67 // in case rewriting urls
68 var base
= $('base').attr('href');
70 // remove anything after the last '/'
71 base
= base
.replace(/\/[^\/]+$/,'/');
73 if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
75 if ($$.attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
76 // use source tag value if set
77 var source
= (base
&& $$.attr('src').search(/^(\/|http:)/i)) ? base
+ $$.attr('src') : $$.attr('src');
79 $$.css({filter
:hack
.filter(source
), width
:$$.width(), height
:$$.height()})
80 .attr({src
:$.ifixpng
.getPixel()})
84 } else { // hack png css properties present inside css
85 var image
= $$.css('backgroundImage');
86 if (image
.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
88 image
= (base
&& image
.substring(0,1)!='/') ? base
+ image
: image
;
89 $$.css({backgroundImage
:'none', filter
:hack
.filter(image
)})
90 .children().children().positionFix();
94 } : function() { return this; };
97 * Removes any png hack that may have been applied previously
99 * $('img[src$=.png]').iunfixpng();
100 * @desc revert hack on all images with png extensions
102 * $('#panel, img[src$=.png]').iunfixpng();
103 * @desc revert hack on element #panel and all images with png extensions
108 $.fn
.iunfixpng
= hack
.ltie7
? function() {
109 return this.each(function() {
111 var src
= $$.css('filter');
112 if (src
.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
114 if ($$.is('img') || $$.is('input')) {
115 $$.attr({src
:src
}).css({filter
:''});
117 $$.css({filter
:'', background
:'url('+src
+')'});
121 } : function() { return this; };
124 * positions selected item relatively
127 $.fn
.positionFix = function() {
128 return this.each(function() {
130 var position
= $$.css('position');
131 if (position
!= 'absolute' && position
!= 'relative') {
132 $$.css({position
:'relative'});