最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get position of map area(html)? - Stack Overflow

programmeradmin3浏览0评论

Is this possible? I'm trying to find the x and y coordinates of the element in relation to the browser.

var position = $(this).position();
    x = position.left;
    y = position.right;

Doesn't work.

Is there any way to do this?

/
highlight the blue room 070

Is this possible? I'm trying to find the x and y coordinates of the element in relation to the browser.

var position = $(this).position();
    x = position.left;
    y = position.right;

Doesn't work.

Is there any way to do this?

http://adamsaewitz./housing/
highlight the blue room 070

Share Improve this question edited Mar 31, 2012 at 17:25 switz asked Dec 25, 2010 at 9:58 switzswitz 25.2k27 gold badges79 silver badges105 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

The problem lies in the fact that you are accessing the top/left of an area element.

The area element is not positioned where its coords say. This is handled behind the scenes by the dom/browser.

So you need to find the image that the area relates to and grab its offset.

var imgId = $(this).closest('map').attr('name');
var imgPos = $('#' + imgId).offset();

Then, you grab the coords attribute of the area and split it to get left/top/width and use those to pinpoint the location inside the image.

var coords = $(this).attr('coords').split(',');
var box = {
            left: parseInt(coords[0],10),
            top: parseInt(coords[1],10),
            width:  parseInt(coords[2],10)-parseInt(coords[0],10),
            height:  parseInt(coords[3],10)-parseInt(coords[1],10)
            };

Take into consideration the width/height of the info box that appears (and since you animate it, take that into consideration as well) and you get to

x = imgPos.left + box.left + box.width/2 - 65; // 65 is the info width/2
y = imgPos.top + box.top -20 -160 -1; // 20 is the animation, 160 is the info height, 1 is a safe distance from the top

demo: http://www.jsfiddle/XBjwN/

Edit for updated question: Since you're using <area> it's a different story, and fetching from the coords attribute is much easier, like this:

var position = $(this).attr('coords').split(',');
x = +position[0] - 50;
y = +position[1] - 170;

The offsets are just to account for the hard-coded width/height of the tooltip itself. In addition to the above, you want to use top and left rather than margin-top and margin-left. Also to account for the #content <div>'s position in the page, give it a relative position for the tooltip to sit in, like this:

#content { position: relative; }

Then...instead of .after(), use .append() so it gets added inside that parent.

You can test the result here.


For original question:
The object .position() returns has top and left properties...but you want .offset() here anyway (it's relative to the document, where .position() is relative to the offset parent), so it should look like this:

var position = $(this).offset(),
    x = position.left,
    y = position.top; //not right!

Or this:

var position = $(this).offset();
var x = position.left;
var y = position.top;

...but without a single var ma-separated statement, or a var on each line, you're also creating (or trying to) global variables, which will blow up in IE.

$(document).ready(function () {
            $('map').imageMapResize();
            $('area').hover(function () {
                $('.imgpopover').css({ "display": "block", "top": $(this).attr("coords").split(',')[1]+"px", "left": $(this).attr("coords").split(',')[0]+"px" })
                $('.imgpopover label').text($(this).attr("title"))
            }, )
        });
发布评论

评论列表(0)

  1. 暂无评论