Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么offsetX和offsetY在firefox和chrome中返回不同的值_Javascript_Html_Css_Svg - Fatal编程技术网

Javascript 为什么offsetX和offsetY在firefox和chrome中返回不同的值

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"> <

我正在编写一个绘图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">
  <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图像{
指针事件:无;
}