为什么 mouseout 和 mouseleave 总是触发
我真的不明白为什么 p
元素总是隐藏的(因此为什么 mouseout
总是触发):
const canvas = document.querySelector("canvas");
const infoBox = document.querySelector("#info-box")
canvas.addEventListener("mousemove", evt => {
evt.preventDefault()
infoBox.style.display="block"
infoBox.style.position="absolute"
infoBox.style.top= evt.clientY+"px"
infoBox.style.left=evt.clientX+"px"
console.log("moved")
})
canvas.addEventListener("mouseout", evt => {
evt.preventDefault()
infoBox.style.display="none"
console.log("exit")
}, false)
canvas{
border-width: 0px;
border-color:#212121;
background-color: blue;
}
<canvas></canvas>
<div id="info-box" style="display: none;"><p>Hello</p></div>
I really don't get why the p
element is always hidden (and therefore why the mouseout
is always firing):
const canvas = document.querySelector("canvas");
const infoBox = document.querySelector("#info-box")
canvas.addEventListener("mousemove", evt => {
evt.preventDefault()
infoBox.style.display="block"
infoBox.style.position="absolute"
infoBox.style.top= evt.clientY+"px"
infoBox.style.left=evt.clientX+"px"
console.log("moved")
})
canvas.addEventListener("mouseout", evt => {
evt.preventDefault()
infoBox.style.display="none"
console.log("exit")
}, false)
canvas{
border-width: 0px;
border-color:#212121;
background-color: blue;
}
<canvas></canvas>
<div id="info-box" style="display: none;"><p>Hello</p></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次出现信息框时都会发生退出。
向
#info-box
div 添加偏移量意味着鼠标认为它没有离开画布。The exit happens every time the info-box appears.
Adding an offset to the
#info-box
div means that the mouse doesn't think it has left the canvas.