Created
April 22, 2015 07:38
-
-
Save zenxds/5b238e7fba554388fab7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define("resizeImageMap", ["jquery"], function($) { | |
var $window = $(window); | |
var dataAttr = "data-src"; | |
$.fn.resizeImageMap = function() { | |
var $images = $(this); | |
var resizeImageMap = function() { | |
$images.each(function() { | |
var $image = $(this), | |
map = $image.attr("usemap"), | |
src = $image.attr("src") || $image.attr(dataAttr); | |
if (typeof map == "undefined") { | |
return; | |
} | |
$image.on("load", function() { | |
// 实际的宽高 | |
var width = this.width, | |
height = this.height; | |
$("<img />").on("load", function() { | |
var originalWidth = this.width; | |
var originalHeight = this.height; | |
$('map[name="' + map.slice(1) + '"]').find("area").each(function() { | |
var $map = $(this); | |
if (!$map.data("coords")) { | |
$map.data("coords", $map.attr("coords")); | |
} | |
var coords = $map.data("coords").split(","), | |
coordsNew = new Array(coords.length); | |
for (var i = 0; i < coordsNew.length; ++i) { | |
if (i % 2 === 0) { | |
coordsNew[i] = parseInt(parseInt(coords[i].trim()) * width / originalWidth); | |
} else { | |
coordsNew[i] = parseInt(parseInt(coords[i].trim()) * height / originalHeight); | |
} | |
} | |
$map.attr("coords", coordsNew.toString()); | |
}); | |
}).attr("src", src).each(function() { | |
if(this.complete) { | |
$(this).load(); | |
} | |
}); | |
}); | |
if ($image.attr(dataAttr)) { | |
$image.attr("src", src).removeAttr(dataAttr); | |
} | |
$image.each(function() { | |
if(this.complete) { | |
$(this).load(); | |
} | |
}); | |
}); | |
}; | |
$window.resize(resizeImageMap); | |
resizeImageMap(); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment