Javascript 为什么offsetX和offsetY在firefox和chrome中返回不同的值
我正在编写一个绘图web应用程序,使用mousemove事件的offsetX和offsetY,它在chrome中运行良好,但在firefox中不起作用Javascript 为什么offsetX和offsetY在firefox和chrome中返回不同的值,javascript,html,css,svg,Javascript,Html,Css,Svg,我正在编写一个绘图web应用程序,使用mousemove事件的offsetX和offsetY,它在chrome中运行良好,但在firefox中不起作用 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 300px;
overflow: auto;
}
</style>
</head>
<body>
<div class="box">
<svg class="svg" width="500" height="500" viewBox="0,0,600,600">
<image xlink:href="https://picsum.photos/600/600" x="0" y="0" width="600" height="600"></image>
</svg>
</div>
<script>
let box = document.querySelector('.svg');
box.addEventListener('mousemove', (e) => {
console.log(e.offsetX, e.offsetY, e.target);
})
</script>
</body>
</html>
文件
.盒子{
宽度:300px;
溢出:自动;
}
let box=document.querySelector('.svg');
box.addEventListener('mousemove',(e)=>{
日志(e.offsetX,e.offsetY,e.target);
})
上面的代码,在chrome中,offsetX在0和500px之间,在firefox中,offsetX在0和600px之间,为什么它们不一样?这是,浏览器不清楚如何计算布局元素的这个值,我们最终会遇到这样的操作间问题
由于需要Chrome的行为,您可以简单地禁用元素上的指针事件,以便直接接收:
let-box=document.querySelector('.svg');
box.addEventListener('mousemove',(e)=>{
console.log(e.offsetX,e.offsetY);
})
.box{
宽度:300px;
溢出:自动;
}
.svg图像{
指针事件:无;
}
这就是,浏览器应该如何计算布局元素的这个值还不清楚,我们最终会遇到这样的交互问题
由于需要Chrome的行为,您可以简单地禁用元素上的指针事件,以便直接接收:
let-box=document.querySelector('.svg');
box.addEventListener('mousemove',(e)=>{
console.log(e.offsetX,e.offsetY);
})
.box{
宽度:300px;
溢出:自动;
}
.svg图像{
指针事件:无;
}