[英]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.