如何使用javascript / jquery获取图像地图的上/左x / y? [英] How to get top/left x/y of image map with javascript / jquery?

查看:200
本文介绍了如何使用javascript / jquery获取图像地图的上/左x / y?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery的position()或offset(),我看不到要获取图像映射区域的顶/左坐标。它在FF工作,但没有别的 - 没有webkit,IE,Opera。

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.

推荐答案

如果要获取顶部和左侧坐标或包含矩形或多边形的块,请尝试此

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); }

获取一个圆的顶部左侧坐标,这取决于您是否需要坐标包含cirlce的块

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 );

或圆圈上最上和下的点

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 );

这篇关于如何使用javascript / jquery获取图像地图的上/左x / y?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆