Subversion Repositories ALCASAR

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2788 rexy 1
/*
2
 * jQuery ifixpng plugin
3
 * (previously known as pngfix)
4
 * Version 2.1  (23/04/2008)+jquery1.9fix
5
 * @requires jQuery v1.1.3 or above
6
 *
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
12
 */
13
 
14
 /**
15
  *
16
  * @example
17
  *
18
  * optional if location of pixel.gif if different to default which is images/pixel.gif
19
  * $.ifixpng('media/pixel.gif');
20
  *
21
  * $('img[@src$=.png], #panel').ifixpng();
22
  *
23
  * @apply hack to all png images and #panel which icluded png img in its css
24
  *
25
  * @name ifixpng
26
  * @type jQuery
27
  * @cat Plugins/Image
28
  * @return jQuery
29
  * @author jQuery Community
30
  */
31
 
32
(function($) {
33
 
34
	/**
35
	 * helper variables and function
36
	 */
37
	$.ifixpng = function(customPixel) {
38
		if (customPixel !== undefined) {
39
		    $.ifixpng.pixel = customPixel;
40
		}
41
	};
42
 
43
	$.ifixpng.getPixel = function() {
44
		return $.ifixpng.pixel || 'images/pixel.gif';
45
	};
46
 
47
	var hack = {
48
		ltie7  : (navigator.userAgent.match(/MSIE ((5\.5)|(6\.))/) !== null),
49
		filter : function(src) {
50
			return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=scale,src='"+src+"')";
51
		}
52
	};
53
 
54
	/**
55
	 * Applies ie png hack to selected dom elements
56
	 *
57
	 * $('img[@src$=.png]').ifixpng();
58
	 * @desc apply hack to all images with png extensions
59
	 *
60
	 * $('#panel, img[@src$=.png]').ifixpng();
61
	 * @desc apply hack to element #panel and all images with png extensions
62
	 *
63
	 * @name ifixpng
64
	 */
65
 
66
	$.fn.ifixpng =  hack.ltie7 ? function(customPixel) {
67
	    if (customPixel !== undefined) {
68
	        $.ifixpng.pixel = customPixel;
69
	    }
70
    	return this.each(function() {
71
 
72
            var filter = $(this).css('filter');
73
            if (!filter.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // if not yet executed
74
    			// in case rewriting urls
75
	    		var base = $('base').attr('href');
76
		    	if (base) {
77
			    	// remove anything after the last '/'
78
			    	base = base.replace(/\/[^\/]+$/,'/');
79
			    }
80
	    		if ($(this).is('img') || $(this).is('input')) { // hack image tags present in dom
81
		    		if ($(this).attr('src')) {
82
			    		if ($(this).attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
83
				    		// use source tag value if set 
84
					    	var source = (base && $(this).attr('src').search(/^(\/|http:)/i)) ? base + $(this).attr('src') : $(this).attr('src');
85
	    					// apply filter
86
		    				$(this).css({filter:hack.filter(source), width:$(this).width(), height:$(this).height()})
87
			    			  .attr({src:$.ifixpng.getPixel()})
88
				    		  .positionFix();
89
			    		}
90
		    		}
91
	    		} else { // hack png css properties present inside css
92
		    		var image = $(this).css('backgroundImage');
93
	    			if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
94
		    			image = RegExp.$1;
95
			    		image = (base && image.substring(0,1)!='/') ? base + image : image;
96
		    			$(this).css({backgroundImage:'none', filter:hack.filter(image)})
97
		    			  .children().children().positionFix();
98
		    		}
99
			    }
100
		    }
101
		});
102
	} : function() { return this; };
103
 
104
	/**
105
	 * Removes any png hack that may have been applied previously
106
	 *
107
	 * $('img[@src$=.png]').iunfixpng();
108
	 * @desc revert hack on all images with png extensions
109
	 *
110
	 * $('#panel, img[@src$=.png]').iunfixpng();
111
	 * @desc revert hack on element #panel and all images with png extensions
112
	 *
113
	 * @name iunfixpng
114
	 */
115
 
116
	$.fn.iunfixpng = hack.ltie7 ? function() {
117
    	return this.each(function() {
118
			var src = $(this).css('filter');
119
			if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
120
				src = RegExp.$1;
121
				if ($(this).is('img') || $(this).is('input')) {
122
					$(this).attr({src:src}).css({filter:''});
123
				} else {
124
					$(this).css({filter:'', background:'url('+src+')'});
125
				}
126
			}
127
		});
128
	} : function() { return this; };
129
 
130
	/**
131
	 * positions selected item relatively
132
	 */
133
 
134
	$.fn.positionFix = function() {
135
		return this.each(function() {
136
			var position = $(this).css('position');
137
			if (position != 'absolute' && position != 'relative') {
138
				$(this).css({position:'relative'});
139
			}
140
		});
141
	};
142
 
143
})(jQuery);