首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有鼠标轮的addEventListener在Firefox中不工作

带有鼠标轮的addEventListener在Firefox中不工作
EN

Stack Overflow用户
提问于 2015-10-21 07:50:24
回答 2查看 2.2K关注 0票数 1

我有一些事件侦听器,它可以在所有浏览器中工作,除了Firefox中的鼠标(在Chrome和其他浏览器中,它工作得很完美)。当我滚动的时候,它应该放大和缩小。这是JSC3D库。代码如下

代码语言:javascript
复制
// setup input handlers.
// compatibility for touch devices is taken into account
var self = this;
if(!JSC3D.PlatformInfo.isTouchDevice) {
    this.canvas.addEventListener('mousedown', function(e){self.mouseDownHandler(e);}, false);
    this.canvas.addEventListener('mouseup', function(e){self.mouseUpHandler(e);}, false);
    this.canvas.addEventListener('mousemove', function(e){self.mouseMoveHandler(e);}, false);
    //this.canvas.addEventListener('mousewheel', function(e){self.mouseWheelHandler(e);}, false);
    this.canvas.addEventListener(JSC3D.PlatformInfo.browser == 'firefox' ? 'DOMMouseScroll' : 'mousewheel', 
                                 function(e){self.mouseWheelHandler(e);}, false);
    document.addEventListener('keydown', function(e){self.keyDownHandler(e);}, false);
    document.addEventListener('keyup', function(e){self.keyUpHandler(e);}, false);
}
else if(JSC3D.Hammer) {
    JSC3D.Hammer(this.canvas).on('touch release hold drag pinch', function(e){self.gestureHandler(e);});
}
else {
    this.canvas.addEventListener('touchstart', function(e){self.touchStartHandler(e);}, false);
    this.canvas.addEventListener('touchend', function(e){self.touchEndHandler(e);}, false);
    this.canvas.addEventListener('touchmove', function(e){self.touchMoveHandler(e);}, false);
}

和函数JSC3D.Viewer.prototype.mouseWheelHandler

代码语言:javascript
复制
JSC3D.Viewer.prototype.mouseWheelHandler = function(e) {
if(!this.isLoaded)
    return;

if(this.onmousewheel) {
    var info = this.pick(e.clientX, e.clientY);
    this.onmousewheel(info.canvasX, info.canvasY, e.button, info.depth, info.mesh);
}

e.preventDefault();
e.stopPropagation();

if(!this.isDefaultInputHandlerEnabled)
    return;

this.zoomFactor *= (JSC3D.PlatformInfo.browser == 'firefox' ? -e.detail : e.wheelDelta) < 0 ? 1.1 : 0.91;
this.update();
};

有没有人?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-23 17:27:54

对于jsc3d最尊敬的地方是,与其嗅探浏览器代理,不如对其进行特性检测,比如:

代码语言:javascript
复制
if(!JSC3D.PlatformInfo.isTouchDevice) {
...
this.canvas.addEventListener('onwheel' in document ? 'wheel' : 'onmousewheel' in document ? 'mousewheel' : 'DOMMouseScroll', function(e){self.mouseWheelHandler(e);});
...
}

编辑:使用相同逻辑的 ...and,还删除“firefox”的检查:

代码语言:javascript
复制
JSC3D.Viewer.prototype.mouseWheelHandler = function(e) {
...
    this.zoomFactor *= (e.deltaY ? e.deltaY : e.wheelDelta ? e.wheelDelta : e.detail) < 0 ? 1.1 : 0.91;
...
};

那么,您应该得到您的处理程序:

代码语言:javascript
复制
function OnViewerMouseWheeel(canvasX, canvasY, button, depth, mesh) {
    ...
}
viewer.onmousewheel=OnViewerMouseWheeel;

在最新的FF中进行了测试,'onwheel' in document正在返回true。

如果这能解决问题请告诉我。

票数 1
EN

Stack Overflow用户

发布于 2015-10-21 07:57:07

代码语言:javascript
复制
if (this.addEventListener) {
	// IE9, Chrome, Safari, Opera
	this.addEventListener("mousewheel", MouseWheelHandler, false);
	// Firefox
	this.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
}
// IE 6/7/8
else this.attachEvent("onmousewheel", MouseWheelHandler);

火狐使用的是DOMMouseScroll事件而不是鼠标轮

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33253741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档