【发布时间】:2021-11-27 04:12:37
【问题描述】:
我正在尝试做的事情:
- 当您将鼠标悬停在行上时,颜色会发生变化并出现 SVG
- 您可以单击SVG(当前在控制台中显示'clicked')来触发一个函数(也需要支持触发外部函数,而不仅仅是那些需要在svg内部的script标签中定义的函数,例如props函数)
- 当您的鼠标离开时,该行将恢复为原始形式
问题:
- 将鼠标悬停在 SVG 按钮上会触发 mouseLeave(离开行) 如果指针事件未设置为无。如果设置为 none,则 SVG 按钮变得无法点击。
- onClick inside SVG 似乎不支持外部函数(props 例如函数)
不胜感激。
const { Container, Row, Col} = ReactBootstrap;
function Library(props){
function handleMouseEnter(e){
e.style.background = 'blue';
var targetNode = e.querySelector(`#content`);
ReactDOM.render(
<svg width="0.75rem" height="0.75rem" className="Layer_1" x="0px" y="0px" viewBox="0 0 472.615 472.615" fill="white">
<polygon onClick={e => console.log('clicked')} points="50.273,0 50.273,472.615 422.342,236.308"/>
</svg>
, targetNode);
}
function handleMouseLeave(e){
e.style.background = '';
var targetNode = e.querySelector(`#content`);
ReactDOM.render(
"hover over me and click the button!"
, targetNode);
}
return(
<Container>
<Row
onMouseEnter={e => handleMouseEnter(e.target.closest('.row'))}
onMouseLeave={e => handleMouseLeave(e.target.closest('.row'))}
>
<Col>
<div id="content">
hover over me and click the button!
</div>
</Col>
</Row>
</Container>
)
}
ReactDOM.render(
<Library/>,
document.getElementById('root')
);
.row{
background:red;
}
svg{
pointer-events:none;
}
<script src="https://unpkg.com/react/umd/react.production.min.js" crossorigin></script>
<script
src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"
crossorigin></script>
<script
src="https://unpkg.com/react-bootstrap@next/dist/react-bootstrap.min.js"
crossorigin></script>
<div id="root"></div>
【问题讨论】:
标签: javascript css reactjs events react-bootstrap