繁体   English   中英

脚本未在Firefox中运行,但已在Chrome中运行

[英]Script not running in Firefox, but ran in Chrome

我编写了一个脚本来注册拇指滚轮事件(如果您想知道的话,是MX Master 2S)。 但是,此脚本在Chrome中运行得很好,但在Firefox(Quantum)中却没有。 为什么会这样?

var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
var elements = document.getElementsByClassName('pagination'); // get the elements
var search = (elements[0].innerHTML.match(regex));

//alert(search);
if(document.addEventListener){
    document.addEventListener("mousewheel", MouseWheelHandler, false);
    document.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
} else {
    document.attachEvent("onmousewheel", MouseWheelHandler);
}

function MouseWheelHandler(e) {
    var e = window.event || e;
  var ret = true;

  if (e.wheelDelta) {
    // Tilt to the left
    if (e.wheelDeltaX < 0) {
        str = window.location.toString();
        strsplit = str.split('/');
        preloc=Number(strsplit[4])+1;
        if (preloc > 0) {
        window.location.replace("https://somepage.com/page/"+preloc);}
        prelocstr=preloc.toString();
        if (prelocstr == "NaN") {
        window.location.replace(search[0]); }
      ret = false;
    }
    // Tilt to the right
    if (e.wheelDeltaX > 0) {
        str = window.location.toString();
        strsplit = str.split('/');
        preloc=Number(strsplit[4])-1;
        if (preloc > 0) {
        window.location.replace("https://somepage.com/page/"+preloc);}
      ret = false;
    }
  }

  event.returnValue = ret;
}

该脚本在Tampermonkey中制作。 谁能指出我的错误? 提前致谢!

有一个用于处理鼠标滚轮事件的新标准,这在所有浏览器中都是标准的:

https://developer.mozilla.org/zh-CN/docs/Web/API/WheelEvent

https://developer.mozilla.org/zh-CN/docs/Web/Events/wheel

要使用此事件,请执行以下操作:

document.addEventListener("wheel", MouseWheelHandler);

并且不需要:

e = window.event || e

活动将在那里。

DOMMouseScroll可与Firefox一起使用,但它使用不同的API。 因此,您必须为Firefox编写一个单独的处理程序,而不是使用MouseWheelHandler ,或者调整MouseWheelHandler以支持两者。

正如kshetline指出的那样,现在有一个新标准可以在所有现代浏览器中使用: https : //developer.mozilla.org/en-US/docs/Web/API/WheelEvent

如下面所述,其他两个选项在Firefox中不起作用:

此功能是非标准的,不在标准轨道上。 请勿在面向Web的生产站点上使用它:它不适用于每个用户。 实现之间也可能存在很大的不兼容性,并且将来的行为可能会更改。

资料来源: https : //developer.mozilla.org/en-US/docs/Web/Events/mousewheel

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM