【发布时间】:2022-08-13 20:53:22
【问题描述】:
以下是使用create-react-app 创建的样板并呈现的唯一组件是TestComponent。在TestComponent 中,它渲染了一个显示几个矩形的 SVG。 (screenshot attached)。
mouseover 事件附加到 SVG 矩形。我的期望是一旦我将鼠标移过矩形,控制台将打印鼠标位置的日志 - (clientX, clientY)。
该行为适用于大多数矩形,但不是全部。 以下矩形正在工作:
- 第 1、第 2、第 5、第 6、第 7 个垂直矩形
3rd and 4th 垂直矩形不打印 clientX 和 clientY。
为了确保这些矩形之间没有重叠,我删除了所有水平矩形。和 mouseover 事件是一样的,第三个和第四个垂直矩形 mouseover 事件不起作用。
我很想相信这是一个浏览器错误,但我不确定。原因是如果我将 stoke-width 更改为 20px,第三个和第四个矩形会响应 mouseover 事件。但是,如果敲击宽度为 1px,那么无论我在这两个矩形上移动多少次,它都没有任何反应。
我正在使用最新的 Chrome 浏览器 - 版本 104.0.5112.81(官方版本)(64 位)。
index.js
import React from \"react\";
import ReactDOM from \"react-dom\";
import \"./index.css\";
import TestComponent from \"./components/test.jsx\";
import reportWebVitals from \"./reportWebVitals\";
const root = document.getElementById(\"root\");
ReactDOM.render(
<React.StrictMode>
<TestComponent />
</React.StrictMode>,
root
);
reportWebVitals();
test.jsx
import React, { Component } from \"react\";
export default class TestComponent extends Component {
onRectMouseOver = (e) => {
console.log(`${e.clientX} ${e.clientY}`);
};
render() {
const rects = JSON.parse(
\"[[0, 45, 45, 2370], [1955, 45, 45, 2370], [355, 45, 45, 2370], [710, 45, 45, 2370], [1065, 45, 45, 2370], [1420, 45, 45, 2370], [1775, 45, 45, 2370], [45, 845, 310.0, 45], [45, 1645, 310.0, 45], [400, 845, 310.0, 45], [400, 1645, 310.0, 45], [755, 845, 310.0, 45], [755, 1645, 310.0, 45], [1110, 845, 310.0, 45], [1110, 1645, 310.0, 45], [1465, 845, 310.0, 45], [1465, 1645, 310.0, 45], [1820, 845, 135.0, 45], [1820, 1645, 135.0, 45], [0, 2415, 2000, 45], [0, 0, 2000, 45]]\"
);
return (
<div style={{ width: 5000, height: 3000, backgroundColor: \"grey\" }}>
<svg
viewBox=\"0 0 5000 3000\"
xmlns=\"http://www.w3.org/2000/svg\"
transform=\"scale(1,-1)\"
>
<g>
{rects &&
rects.map((rect, index) => {
let [x, y, width, height] = rect;
return (
<rect
x={x}
y={y}
width={width}
height={height}
key={index}
style={{ fill: \"None\", strokeWidth: 1, stroke: \"black\" }}
onMouseOver={(e) => {
this.onRectMouseOver(e);
}}
>
<title>
x: {x}; y: {x}; width: {width}; height:
{height}
</title>
</rect>
);
})}
</g>
</svg>
</div>
);
}
}
-
我确实检查了 Z 轴,当我将鼠标移到第三个和第四个垂直矩形上时,我的鼠标路径上没有第二层 @RobertLongson
-
我在您的代码中使用了 const rects 来生成 svg。结果与您问题中的图像不同。
viewBox=\"0 0 5000 3000\"也不适用于您拥有的 svg。您是否使用正确的代码? -
我使用了与 div 相同大小的 viewBox,所以比例是 1:1。横梁也使用@enxaneta。代码版本对应截图。就屏幕截图而言,代码是“正确的”,但对于鼠标悬停事件却是不正确的。
-
@RobertLongson 删除了所有水平矩形,以便这些矩形之间没有覆盖。结果是一样的。
-
@RobertLongson @enxaneta 我非常相信这是一个 Chrome 错误。我已通过 Chrome
report an issue向 Chrome 开发团队提出了这个问题。
标签: javascript html reactjs svg dom-events