可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to resize an image map on window resize event. The closest I've gotten is to use a mouseclick event, but it needs to be window resize for what I'm doing. I'm using Firefox 3.5.5
I'm using jquery somewhat. Here's my example - the area button I want to resize on window resize is in the top left (click on it to resize map and area button):
http://www.whitebrickstudios.com/foghornstour/imagemap3.html
Any help would be appreciated!
Thank you,
Rich
回答1:
I wrote some simple function to rebuild all map points on every event. Try this
function mapRebuild(scaleValue) {
var map = $("#imgmap"); // select your map or for all map you can choose $("map").each(function() { map = $(this);.....})
map.find("area").each(function() { // select all areas
var coords = $(this).attr("coords"); // extract coords
coords = coords.split(","); // split to array
var scaledCoords = "";
for (var coord in coords) { // rebuild all coords with scaleValue
scaledCoords += Math.floor(coords[coord] * scaleValue) + ",";
}
scaledCoords = scaledCoords.slice(0, -1); // last coma delete
$(this).attr("coords", scaledCoords); // set new coords
});
}
scaleValue can be calculated as oldWindowWidth/newWindowWidth. Of course you need to keep the value of oldWindowWidth on window resize. Maybe my solution not on time, but i hope this is useful to someone
回答2:
I think what you want is at
http://home.comcast.net/~urbanjost/semaphore.html
where I show different examples of how to make your image map coordinates change when your image display size changes.
回答3:
This is an old thread but for anyone looking for a solution for a similar or even identical problem, the ImageMapster jQuery plugin appears to provide the easiest solution. You can use its resize method (which can even animate the resizing if desired!) as follows to resize an image along with its image map:
$('img').mapster( 'resize', newWidth, newHeight, resizeTime);
You can find a link on ImageMapster's demo page to a jsFiddle that demonstrates resizing an image & its map in response to changing the browser window.
回答4:
As a modified version of Viktor's answer, this version can handle multiple resizes. It stores initial values for comparison against any future resize. This also uses waitForFinalEvent so it doesn't run over and over on resize.
var mapImg = $('#mapImg');
var naturalWidth = 1200; // set manually to avoid ie8 issues
var baseAreas = new Array();
var scaleValue = mapImg.width() / naturalWidth;
$(window).resize( function() {
waitForFinalEvent( function() {
scaleValue = mapImg.width() / naturalWidth;
mapRebuild( scaleValue );
}, 500, 'resize-window');
});
function mapRebuild( scaleValue ) {
var map = $("#imgMap");
var mapareas = map.find( 'area' );
if ( baseAreas.length == 0 ) {
mapareas.each( function() {
baseAreas.push( $(this).attr( 'coords' ) ); // set initial values
});
}
mapareas.each( function( index ) {
var coords = baseAreas[index]; // use the corresponding base coordinates
coords = coords.split( ',' );
var scaledCoords = '';
for ( var coord in coords ) {
scaledCoords += Math.floor( coords[coord] * scaleValue ) + ',';
}
scaledCoords = scaledCoords.slice( 0, -1 );
$(this).attr( 'coords', scaledCoords );
});
}
mapRebuild( scaleValue ); // initial scale
回答5:
To call a function when the window is resized, try the following:
$(window).bind('resize', function() {
// resize the button here
});
Also, line 37 is missing a dollar sign:
scaleXY('theMap',(window).width());
It should be:
scaleXY('theMap',$(window).width());
回答6:
If you only need to resize the image, use this technique:
http://www.cssplay.co.uk/layouts/background.html
Thanks to CSSPlay.