17

I want to display an overlay (html div) when a user clicks on an element in an SVG diagram. To visualize the problem I'm having, suppose that the SVG image has a horizontal row of 6 elements. At the click event, I get the element's coordinates and use them to display the overlay next to it. The problem is that as I click the elements from left to right, I notice that the horizontal offset between the element and the overlay keeps getting smaller. That is, the 6th element displays the overlay much closer to it than the first element. This happens in both Chrome and FF, and it's an issue because sometime the overlay covers the element itself.

At first I was using JQuery's position() property, which didn't exhibit the behavior that I described above, but it returned very different values in Chrome and Firefox, plus it is not officially supported by JQuery on svg elements. So I tried with DOM's standard offsetLeft and offsetTop, as well as svg's x.animVal.value property and various libraries that I found on the web, but they all have the same erratic offset problem. I presume that this happens because the svg image is scaled, so I'm looking for I way to just get an svg's element position relative to the actual html document which contains it. Is there a way to do this?

3 Answers 3

36

In case you haven't worked something out since March (and for anyone else having this problem), try getBoundingClientRect() on your SVG node.

Returns a ClientRect object that gives you top, bottom, left, right, width, and height relative to the document. Was able to use this to position Twitter Bootstrap popovers (divs) next to SVG rects.

3
12

jQuery's position() does not work well for SVG elements. There is a ticket for that.

You can use the native SVG method getBBox() to get the position of a SVG element.

Example

$('svg circle')[0].getBBox();
1
  • 14
    getBBox() tells you the size of the bounding box for that element, but it is only accurate if the SVG is at 100% scale.
    – Andy
    Commented Nov 3, 2014 at 20:51
11

You can get the position coordinate relative to the page of any element, also <svg>, with this little function:

function getOffset(element)
{
    var bound = element.getBoundingClientRect();
    var html = document.documentElement;

    return {
        top: bound.top + window.pageYOffset - html.clientTop,
        left: bound.left + window.pageXOffset - html.clientLeft
    };
}

var offset = getOffset(svg);
var x = offset.left;
var y = offset.top;

live demo: https://codepen.io/martinwantke/pen/rpNLWr

2
  • 1
    Awesome! Many thanks - I put too much time today to display a tooltip over the data element inside svg. I had it semi working depending on the mouse position but the requirements changed and your solution came real handy.
    – codeepic
    Commented Jul 3, 2018 at 16:23
  • Thanks for sharing, but what if you want the offset of one SVG element to the SVG document?
    – Crashalot
    Commented Dec 20, 2018 at 2:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.