views:

147

answers:

2

Using jQuery's position() or offset(), I can't seeme to get the top/left coordinates of an image map area. It works in FF, but nothing else - no webkit, IE, Opera.

$('area').bind("click",function(){
 alert($(this).position().left);
}); 

<area shape="rect" coords="14,25,205,150" href="#">

Anyone know of a different way to access these? Normally I would just take the coords and split(",") but there are a bunch of multi-faceted area's on these pages.

A: 
$('area').click(function() { 
var url = $(this).attr('href'); 
var coords = $(this).attr('coords').split(','); 

// Your code here 

// To prevent default action 
  return false; 
});
XGreen
but how would that deal with coordinates that aren't simple rectangles? area shape="rect" coords="14,25,205,150,352,123,24,242,532"
jpea
+1  A: 

If you want to get the top and left coordinate or the block that contains the rectangle or polygon, try this

var i, x = [], y = [];
var c = $(this).attr('coords').split(',');
for (i=0; i < c.length; i++){
 x.push( c[i++] );
 y.push( c[i] );
}
var t = y.sort(num)[0];
var l = x.sort(num)[0];
alert( 'top = ' + t + ', left = ' + l );
function num(a, b){ return (a-b); }

to get the top, left coordinate of a circle it depends on if you want the coordinates of the block that contains the cirlce

var c = $(this).attr('coords').split(',');
var t = c[1] - c[2];
var l = c[0] - c[2];
alert( 'top = ' + t + ', left = ' + l );

or the point on the circle that is both the furthest top and left

var c = $(this).attr('coords').split(',');
var t = parseFloat(c[1]) - parseFloat(c[2]) * Math.cos(r);
var l = parseFloat(c[0]) + parseFloat(c[2]) * Math.sin(r);
alert( 'top = ' + t + ', left = ' + l );
fudgey
awesome, thanks for the help. just for clarity, when you used "num" in the first bit, i'm assuming you were referencing a function to numerically sort the array? eg: function num(a, b){ return (a-b); }
jpea
@jpea: Ah, yes... I must have left that function out of the code by accident.
fudgey