简体   繁体   English

查找相对于svg元素的鼠标指针位置

[英]Finding the mouse pointer position relative to an svg element

I have a scg element written as below. 我有一个scg元素,如下所示。

<svg height="300px" width="600px" xmlns="http://www.w3.org/2000/svg">
  <g id="100" onmouseout="outg()" style="stroke-width: 1;fill: rgb(255, 200, 200);">
   <rect height="25" rx="10" ry="10" style="fill: rgb(257, 87, 87);" width="160" x="330" y="35" />
    <text style="stroke: rgb(0, 0, 0);stroke-width: 1;" x="390" y="53" >ABC</text>
  </g>
</svg>

How do i determine if the mouse pointer is inside or outside the rectangle(but inside the svg). 如何确定鼠标指针是在矩形内部还是外部(但在svg内部)。 When I tried calling javascript functions with simple alerts for the onmouseout events for 'rect','g','text', it gets a bit confusing. 当我尝试使用简单的警告来调用javascript函数时,对于'rect','g','text'的onmouseout事件,它会让人感到有些困惑。 Within the rectanle when I point over the text, technically the rectangle is exited and the text is entered. 当我指向文本时,在矩形内,技术上退出矩形并输入文本。

I need to determine that the mouse pointer is completely out of the rectangle and this must happen only once, not on crossing over to the text area within the rectangle or out of the text area into the rectangle. 我需要确定鼠标指针完全在矩形之外,这必须只发生一次,而不是跨越到矩形内的文本区域或者从文本区域进入矩形。

If you're using jQuery, you can make use of the jQuery mouseleave/mouseenter events, which do not have this problem. 如果您正在使用jQuery,则可以使用没有此问题的jQuery mouseleave / mouseenter事件。

the jQuery API docs describe it pretty well: jQuery API文档很好地描述了它:

The mouseleave event differs from mouseout in the way it handles event bubbling. mouseleave事件与mouseout处理事件冒泡的方式不同。 If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. 如果在此示例中使用了mouseout,那么当鼠标指针移出Inner元素时,将触发处理程序。 This is usually undesirable behavior. 这通常是不受欢迎的行为。 The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. 另一方面,mouseleave事件仅在鼠标离开它所绑定的元素时触发其处理程序,而不是后代。 So in this example, the handler is triggered when the mouse leaves the Outer element, but not the Inner element. 因此,在此示例中,处理程序在鼠标离开Outer元素时触发,而不是Inner元素。

Source, with examples: http://api.jquery.com/mouseleave/ 来源,举例: http//api.jquery.com/mouseleave/

If you're not using jQuery and you're willing to do a bit of hacking, I'd suggest taking a look at the source for the jQuery mouseleave event , which will hopefully point you in the right direction. 如果你不使用jQuery并且你愿意做一些黑客行为,我建议你看一下jQuery mouseleave事件的来源,希望能指出你正确的方向。

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

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