How to get the click coordinates relative to SVG element holding the onclick listener?

前端 未结 3 1010
萌比男神i
萌比男神i 2020-12-25 11:37

I haven\'t been able to calculate the click coordinates (x and y) relative to the element triggering the event. I have not found an easy example online.

I have a sim

3条回答
  •  粉色の甜心
    2020-12-25 12:13

    Try to use getBoundingClientRect(): http://jsfiddle.net/fLo4uatw/

    function clicked(evt){
        var e = evt.target;
        var dim = e.getBoundingClientRect();
        var x = evt.clientX - dim.left;
        var y = evt.clientY - dim.top;
        alert("x: "+x+" y:"+y);
    }  
    

提交回复
热议问题