Firefox 3.6 命中测试重叠 SVG 形状

发布于 2024-09-07 01:25:16 字数 1696 浏览 5 评论 0原文

我正在绘制 2 个 SVG 路径,就像 Google 地图对方向所做的那样。

我遇到的问题是,第一条路径与第二条路径的边界框重叠的部分不会触发鼠标悬停、鼠标移出或单击事件。即使事件处理程序位于路径 dom 元素上,当鼠标位于路径边界框内的任何位置时,事件似乎都会触发。

有什么办法可以避免这种情况吗?在 Chrome、Safari 和 Opera 上我没有这个问题。 Firefox 中只有 SVG。

我创建了一个测试示例。当您将鼠标移到圆圈上时,它应该会提醒您它的颜色。您会发现,如果您从最右侧接近绿色圆圈,它会起作用,因为它落在红色圆圈容器的 300px 宽度之外。但是,如果您从绿色圆圈的顶部或底部接近,您将一无所获。现在在 Chrome/Safari/Opera 中尝试同样的操作,您会发现它按预期工作。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:svg="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink"> 
<head> 
 <title>SVG embedded inline in XHTML</title>

 <script type="text/javascript">
  window.onload = function () {
   var red = document.getElementById('red-circle');
   var green = document.getElementById('green-circle');


   red.onmouseover = function (e) {
    alert('red mouse over');
   };
   green.onmouseover = function (e) {
    alert('green mouse over');
   };
  };
 </script>
</head> 
<body> 

 <svg:svg style="position: absolute; top: 0px; left: 110px; z-index: 0;" version="1.1" baseProfile="full" width="300px" height="200px">
  <svg:circle id="green-circle" cx="150px" cy="100px" r="50px" fill="green" /> 
 </svg:svg>  

 <svg:svg style="position: absolute; top: 0px; left: 0px; z-index: 0;" version="1.1" baseProfile="full" width="300px" height="200px">
  <svg:circle id="red-circle" cx="150px" cy="100px" r="50px" fill="#ff0000" /> 
 </svg:svg>
</body> 
</html> 

I am drawing 2 SVG paths much like Google Maps does with directions.

The problem I have is that the section of the first path that is overlapped by the bounding box of the second path won't fire the mouseover, mouseout or click events. Even though the event handlers are on the path dom element the events seem to fire when the mouse is anywhere inside the the bounding box of the path.

Is there anyway to avoid this? On Chrome, Safari and Opera I don't have this problem. It is only SVG in Firefox.

I've created a test example. When ever you move the mouse over a circle it is supposed to alert you of it's colour. You will see that if you approch the green circle from the very right side it works because it falls outside the 300px width of the red circle container. However if you approach from the top or bottom of the green circle you will get nothing. Now try the same thing in Chrome/Safari/Opera and you will see that it works as expected.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:svg="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink"> 
<head> 
 <title>SVG embedded inline in XHTML</title>

 <script type="text/javascript">
  window.onload = function () {
   var red = document.getElementById('red-circle');
   var green = document.getElementById('green-circle');


   red.onmouseover = function (e) {
    alert('red mouse over');
   };
   green.onmouseover = function (e) {
    alert('green mouse over');
   };
  };
 </script>
</head> 
<body> 

 <svg:svg style="position: absolute; top: 0px; left: 110px; z-index: 0;" version="1.1" baseProfile="full" width="300px" height="200px">
  <svg:circle id="green-circle" cx="150px" cy="100px" r="50px" fill="green" /> 
 </svg:svg>  

 <svg:svg style="position: absolute; top: 0px; left: 0px; z-index: 0;" version="1.1" baseProfile="full" width="300px" height="200px">
  <svg:circle id="red-circle" cx="150px" cy="100px" r="50px" fill="#ff0000" /> 
 </svg:svg>
</body> 
</html> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⒈起吃苦の倖褔 2024-09-14 01:25:16

我可以使用 Firefox 4 重现这一点。您可以做的就是在 DOM 层次结构中的更高级别安装事件处理程序,并使用事件的 target 字段来区分图形元素。

I can reproduce this with Firefox 4. What you can do is install the event handler at a higher level in the DOM hierarchy and use the event's target field for discerning between the graphic elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文