Skip to content

Commit 369c574

Browse files
committed
IE8 support (HT @dave-irvine), maintain chainability, optimisations
1 parent 72e6b06 commit 369c574

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

jquery.rwdImageMaps.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* rwdImageMaps jQuery plugin v1.2
2+
* rwdImageMaps jQuery plugin v1.3
33
*
44
* Allows image maps to be used in a responsive design by recalculating the area coordinates to match the actual image size on load and window.resize
55
*
@@ -8,28 +8,31 @@
88
* http://mattstow.com
99
* Licensed under the MIT license
1010
*/
11-
(function($) {
11+
;(function($) {
1212
$.fn.rwdImageMaps = function() {
13-
var $img = this;
14-
var v = parseFloat($.fn.jquery);
13+
var $img = this,
14+
version = parseFloat($.fn.jquery);
1515

1616
var rwdImageMap = function() {
1717
$img.each(function() {
1818
if (typeof($(this).attr('usemap')) == 'undefined')
1919
return;
2020

21-
var that = this;
22-
var $that = $(that);
21+
var that = this,
22+
$that = $(that);
2323
// Since WebKit doesn't know the height until after the image has loaded, perform everything in an onload copy
24-
$('<img />').attr('src', $that.attr('src')).load(function() {
25-
var w, h;
24+
$('<img />').load(function() {
25+
var w,
26+
h,
27+
attrW = 'width',
28+
attrH = 'height';
2629
// jQuery < 1.6 incorrectly uses the actual image width/height instead of the attribute's width/height
27-
if (v < 1.6)
28-
w = that.getAttribute('width'),
29-
h = that.getAttribute('height');
30+
if (version < 1.6)
31+
w = that.getAttribute(attrW),
32+
h = that.getAttribute(attrH);
3033
else
31-
w = $that.attr('width'),
32-
h = $that.attr('height');
34+
w = $that.attr(attrW),
35+
h = $that.attr(attrH);
3336

3437
var wPercent = $that.width()/100,
3538
hPercent = $that.height()/100,
@@ -41,8 +44,8 @@
4144
if (!$this.data(c))
4245
$this.data(c, $this.attr(c));
4346

44-
var coords = $this.data(c).split(',');
45-
var coordsPercent = new Array(coords.length);
47+
var coords = $this.data(c).split(','),
48+
coordsPercent = new Array(coords.length);
4649

4750
for (var i = 0; i < coordsPercent.length; ++i) {
4851
if (i % 2 === 0)
@@ -52,9 +55,11 @@
5255
}
5356
$this.attr(c, coordsPercent.toString());
5457
});
55-
});
58+
}).attr('src', $that.attr('src'));
5659
});
5760
};
5861
$(window).resize(rwdImageMap).trigger('resize');
62+
63+
return this;
5964
};
6065
})(jQuery);

jquery.rwdImageMaps.min.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* rwdImageMaps jQuery plugin v1.2
2+
* rwdImageMaps jQuery plugin v1.3
33
*
44
* Allows image maps to be used in a responsive design by recalculating the area coordinates to match the actual image size on load and window.resize
55
*
@@ -8,4 +8,4 @@
88
* http://mattstow.com
99
* Licensed under the MIT license
1010
*/
11-
(function($){$.fn.rwdImageMaps=function(){var $img=this;var v=parseFloat($.fn.jquery);var rwdImageMap=function(){$img.each(function(){if(typeof($(this).attr("usemap"))=="undefined"){return}var that=this;var $that=$(that);$("<img />").attr("src",$that.attr("src")).load(function(){var w,h;if(v<1.6){w=that.getAttribute("width"),h=that.getAttribute("height")}else{w=$that.attr("width"),h=$that.attr("height")}var wPercent=$that.width()/100,hPercent=$that.height()/100,map=$that.attr("usemap").replace("#",""),c="coords";$('map[name="'+map+'"]').find("area").each(function(){var $this=$(this);if(!$this.data(c)){$this.data(c,$this.attr(c))}var coords=$this.data(c).split(",");var coordsPercent=new Array(coords.length);for(var i=0;i<coordsPercent.length;++i){if(i%2===0){coordsPercent[i]=parseInt(((coords[i]/w)*100)*wPercent)}else{coordsPercent[i]=parseInt(((coords[i]/h)*100)*hPercent)}}$this.attr(c,coordsPercent.toString())})})})};$(window).resize(rwdImageMap).trigger("resize")}})(jQuery);
11+
;(function(a){a.fn.rwdImageMaps=function(){var d=this,c=parseFloat(a.fn.jquery);var b=function(){d.each(function(){if(typeof(a(this).attr("usemap"))=="undefined"){return}var f=this,e=a(f);a("<img />").load(function(){var i,k,l="width",g="height";if(c<1.6){i=f.getAttribute(l),k=f.getAttribute(g)}else{i=e.attr(l),k=e.attr(g)}var j=e.width()/100,n=e.height()/100,m=e.attr("usemap").replace("#",""),o="coords";a('map[name="'+m+'"]').find("area").each(function(){var r=a(this);if(!r.data(o)){r.data(o,r.attr(o))}var q=r.data(o).split(","),p=new Array(q.length);for(var h=0;h<p.length;++h){if(h%2===0){p[h]=parseInt(((q[h]/i)*100)*j)}else{p[h]=parseInt(((q[h]/k)*100)*n)}}r.attr(o,p.toString())})}).attr("src",e.attr("src"))})};a(window).resize(b).trigger("resize");return this}})(jQuery);

0 commit comments

Comments
 (0)