繁体   English   中英

如何根据文档获取SVG中元素的坐标?

[英]How can I get the coordinates of an element inside SVG according to the document?

在jQuery中,您可以执行

$('div').offset()

并根据文档返回元素的位置。

不幸的是,这不适用于SVG元素。

有什么方法可以像正常DOM元素那样获得SVG元素的绝对位置吗?

可以通过使用clientX,clientY加上pageXOffset,pageYOffset来确定。

在html页面中尝试:

<div id="svgDiv" style='width:400px;height:400px;'>
    <svg id="mySVG" width="400" height="400">
        <rect id="myRect" onmouseover="showMyComment(evt)"  onmouseout="hideMyComment()" x="100" y="100" width="100" height="100" fill="blue" />
    </svg>
</div>
<div style='position:absolute;visibility:hidden' id="myCommentDiv">This is my location</div>

与代码:

function showMyComment(evt)
{
    x=evt.clientX + window.pageXOffset
    y=evt.clientY + window.pageYOffset
    myCommentDiv.style.left=x+"px"
    myCommentDiv.style.top=y+"px"
    myCommentDiv.style.visibility="visible"
}
function hideMyComment()
{
  myCommentDiv.style.visibility="hidden"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM