Javascript "onmousemove()"在 Chrome 中有效,但在 Firefox 中无效

标签 javascript html firefox canvas

我已经尝试过谷歌搜索,但找不到任何解决方案。

当鼠标悬停在底部的四个塔上时,它们应该显示不同的图像(不太透明)。这在 Google Chrome 中完美运行,但在 Firefox 中则不行。

这是网站:http://cdelphinus.com/test/mormontowerdefense.html (您可以右键单击->查看源代码)查看整个代码(app.js)

这是问题代码:

在我的 HTML 中:

<canvas id="Canvas_One" width="1160" height="580" style="border:2px solid yellow;          background-color:black; padding-left:0;margin-left:0;" onmousemove="lolol(event)"    onclick="haha(event)"></canvas>

在我的 app.js 文件中:

这是我的问题的一个非常浓缩的版本,这在 Chrome 中有效,但在 Firefox 中无效:

//function lolol(event) {
//  event = event || window.event;
//      alert(event.x);
//}

我有一种感觉,一旦上面的警报出现在 Firefox 中,其余的就会神奇地修复。

这是正在发生的事情的更详细版本

function lolol(event) {
event = event || window.event;
ix = event.x - canvas.offsetLeft + 10;  
iy = event.y- canvas.offsetTop  + document.body.scrollTop;
//...
if(iy > 510) {
    var dist = Math.round(ix / 50) - 1;
    if(dist < towerpurchaseoptions.length){
    towerpurchaseoptions[dist].isActive = true;
    //textout(towerpurchaseoptions[dist].name);
     for(var i=0; i<towerpurchaseoptions.length; i++) {
         if(i != dist) {
    towerpurchaseoptions[i].isActive = false;
         }
     }
}  
}
//....
}

如果 towerpurchaseoptions[n].isActive == true,则图像将替换为透明度较低的图像

最佳答案

我检查了你的问题,这只是因为

function lolol(event) {
     event = event || window.event; //For IE

    ix = event.x - canvas.offsetLeft + 10;

//这里 ix 正在转 nan

    iy = event.y- canvas.offsetTop  + document.body.scrollTop;

//这里 iy 正在旋转 nan

那为什么呢? 因为事件在 Firefox 中没有属性 .x 可能会出现一些问题,您可以将其更改为

 function lolol(event) {
         event = event || window.event; //For IE

        ix = event.clientX - canvas.offsetLeft + 10;


        iy = event.clientY- canvas.offsetTop  + document.body.scrollTop;

它会起作用,谢谢。

关于Javascript "onmousemove()"在 Chrome 中有效,但在 Firefox 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24444147/

相关文章:

javascript - Firefox 'Cross-Origin Request Blocked' 尽管 header

javascript - 使用 JavaScript 读取元素的 CSS 属性

javascript - 如何从通过 https 加载的网页与桌面应用程序通信

javascript - 如何从常规 Javascript 函数访问 angularjs 常量

javascript - 在HTML5 Canvas 上慢慢绘制二次曲线

javascript - 使用 margin auto 使文本框居中

html - DIV 缩放后变为矩形

javascript - 如何添加代码来计算价格并将其连接到 orderMessage

internet-explorer - 在 Chrome/Firefox/IE 中通过 iframe 打印 PDF

javascript - [1,2,3].slice(1,undefined) 的值应该是多少?