简体   繁体   English

Javascript函数不适用于Firefox,但适用于Chrome,Safari,IE和Edge

[英]Javascript function does not work in firefox but does in chrome, safari, IE and edge

Could anyone tell me why this function won't execute in Firefox. 谁能告诉我为什么该功能无法在Firefox中执行。 The rest of the Javascript functions throughout the file work successfully except for this one (it flat out won't execute, no errors in the console). 除此功能外,整个文件中的其余Javascript函数均能正常工作(它不会执行,控制台中不会出错)。 I suspected it was the pageX property, however that doesn't seem to be the case. 我怀疑这是pageX属性,但是事实并非如此。

var prevX = 0;
var i = 0;
var drgleft = 0;
var drgright = 0;


function sequence_1(event){
    if(prevX == 0){
        prevX = event.pageX;
        return false;
    }  
    //drag left
    if(prevX > event.pageX){
        console.log('dragged left');
        drgleft++;
        if(drgleft == 2){
            drgleft = 0;
            i--;
            if(i < 0){
                i = 30; //for optimization reasons, input the cache.length value manually (this avoids unnecessary errors in the console and laggy framerate as a result).
            } 
            document.getElementById("TheBigOne").src = cache[i].src; //use console.log(i); as a method of verifying that the code is executing correctly
        }
    }
    else if(prevX < event.pageX){
        console.log('dragged right');
        drgright++;
        if(drgright == 2){
            drgright = 0;
            i++;
            if(i > 30){ //for optimization reasons, input the cache.length value manually (this avoids unnecessary errors in the console and laggy framerate as a result).
                i=0;
            }
            document.getElementById("TheBigOne").src = cache[i].src;
        }
    }
    else{
    }
    prevX = event.pageX
}

EDIT: 编辑:

In my first post I didn't include the relevent HTML code that calls the function, for that I apologise and will implement it below. 在我的第一篇文章中,我没有包含调用该函数的相关HTML代码,对此我深表歉意,并将在下面实现。

 <!DOCTYPE html>
    <html>
       <body>
          <div class="The_main_event" draggable = "true" ondrag="sequence_1(event)" id = "GD">

              <img src="file:///C:/Users/Harry/Desktop/Website/Web_aeroplane/Web%20Test.0031.png" id="TheBigOne">

           </div>
    </body>
  </html>

Open Firefox's debugger ( Ctrl + Shift + S on Windows) set a breakpoint at the first line of the function body. 打开Firefox的调试器(在Windows上为Ctrl + Shift + S ),在函数正文的第一行设置一个断点。 The contents of event will be inspectable. event的内容将是可检查的。 You can step through the function ( F10 in Windows) to see what's happening. 您可以单步执行该功能(Windows中为F10 )以查看发生了什么。 If the event is not directly from mouse/touch, it may not have a pageX property, therefore have no effects. 如果该事件不是直接来自于鼠标/触摸,则它可能没有pageX属性,因此无效。

Is it a UIEvent, or a MouseEvent? 是UIEvent还是MouseEvent?

https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/pageX https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/pageX

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/pageX https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/pageX

UIEvents aren't standard and the documentation linked above has Firefox as unknown compatibility. UIEvents不是标准的,并且上面链接的文档将Firefox作为未知兼容性。

I'm guessing when you say it won't run, you mean, it's not working how you expect it to. 我猜您何时说它不会运行,您的意思是,它无法按预期运行。 It looks like in your case it is either returning false, or is doing nothing except trying to update prevX . 在您的情况下,它看起来要么返回false,要么什么都不做,除了尝试更新prevX If event.pageX is undefined and therefore prevX is undefined , it would cause your function to have no effect and appear to not run. 如果event.pageXundefined ,因此prevXundefined ,这将导致您的函数无效并似乎无法运行。

EDIT: After seeing your update, it looks like it's because the event you're listening to doesn't have pageX. 编辑:看到您的更新后,似乎是因为您正在监听的事件没有pageX。 See here for documentation: https://developer.mozilla.org/en-US/docs/Web/Events/drag 参见此处获取文档: https : //developer.mozilla.org/zh-CN/docs/Web/Events/drag

This may also help FireFox onDrag Pagex PageY always zero ;( 这也可能有助于FireFox onDrag Pagex PageY始终为零;(

Unfortunately this is not supported by firefox Browser and W3C. 不幸的是,firefox浏览器和W3C不支持此功能。 Other browsers ignore this specification and use Mousemove instead, to address this specific issue. 其他浏览器忽略此规范,而是使用Mousemove来解决此特定问题。 However you can use ondragstart and ondragend to get the co-ordinates of the specific event. 但是,您可以使用ondragstartondragend来获取特定事件的坐标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么javascript函数在firefox中可以工作,但在Chrome或IE上却不能工作 - Why does a javascript function work in firefox but not work on Chrome or IE 为什么我的Javascript / jQuery可以在Chrome和Safari中运行,但不能在Firefox,IE9或Opera中运行? - Why does my Javascript/jQuery work in Chrome and Safari but not firefox, IE9, or Opera? onclick javascript函数不适用于Firefox / IE - onclick javascript function does not work with Firefox/IE Firefox Javascript div旋转不起作用,但在Chrome和Safari中可以正常工作 - Firefox Javascript div rotation not working, but does work in Chrome and Safari JavaScript 在 Safari 中不起作用,但在 Firefox 和 Chrome 中起作用 - JavaScript doesn't work in Safari but does in Firefox and Chrome 为什么这段JavaScript既可以在Google Chrome和IE上运行,又不能在Firefox中运行? - Why does this piece of javascript work on both Google Chrome and IE but not in Firefox? 锚标记中的Javascript在FireFox和IE中不起作用。 可以在Chrome浏览器中使用 - Javascript in anchor tag not working in FireFox and IE. Does work in Chrome 为什么instanceof在Chrome,Safari和Edge上返回false,在FireFox上返回true? - Why does instanceof return false on Chrome, Safari and Edge and true on FireFox? Onclick在IE或Firefox中无法使用,但在Chrome中可以使用 - Onclick not working in IE or Firefox but does work in Chrome 为什么这适用于 IE,但不适用于 Firefox 或 Chrome? - Why does this work for IE but not for Firefox or Chrome?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM