Kev*_*Bot 4 javascript google-chrome reactjs
注意:这似乎是 Chrome 独有的问题
onMouseEnter当阻塞事件元素的元素消失时,React 目前不会触发该事件。标准 JS 事件甚至委托事件都不是这种情况。
这是我遇到的问题的简化描述和代码示例;当下部元素的工具提示消失(并且光标位于上部元素上方)时,onMouseEvent不会触发:
function reactMouseEnter({currentTarget}) {
console.log('React Mouse Enter Event');
const rect = currentTarget.getBoundingClientRect();
const tooltip = document.createElement('div');
tooltip.classList.add('tooltip');
tooltip.style.top = rect.y - 30 + 'px';
document.body.appendChild(tooltip);
const mouseLeaveHandler = () => {
currentTarget.removeEventListener('mouseleave', mouseLeaveHandler);
setTimeout(() => {
tooltip.parentNode.removeChild(tooltip);
}, 300);
};
currentTarget.addEventListener('mouseleave', mouseLeaveHandler);
}
class Example extends React.Component {
ref = React.createRef()
componentDidMount() {
this.ref.current.addEventListener('mouseenter', () => {
console.log('Regular Mouse Enter Event');
})
}
render() {
return (
<div>
<div onMouseEnter={ reactMouseEnter } ref={this.ref} className="box" />
<div onMouseEnter={ reactMouseEnter } className="box" />
</div>
);
}
}
ReactDOM.render(<Example /> , document.getElementById('example'));Run Code Online (Sandbox Code Playgroud)
.box {
border: 1px solid black;
height: 28px;
margin-bottom: 10px;
position: relative;
width: 28px;
}
.tooltip {
width: 30px;
height: 30px;
background-color: lightcoral;
position: absolute;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.3.2/umd/react-dom.production.min.js"></script>
<div id="example" style="margin-top: 30px;"></div>Run Code Online (Sandbox Code Playgroud)
我可以在 React 中做一些不同的事情来开始onMouseEnter工作吗?我也尝试使用onMouseOver,但它在我的实际实现中产生了其他意想不到的副作用,所以更愿意看看这是否可以用onMouseEnter.
| 归档时间: |
|
| 查看次数: |
6048 次 |
| 最近记录: |