2015-10-21 64 views
1

我有幾個事件監聽器,它適用於所有瀏覽器,除了Firefox中的鼠標滾輪(在Chrome和其他版本中都是完美的)。滾動時,它應該放大和縮小。這是JSC3D庫。代碼如下帶鼠標滾輪的addEventListener在Firefox中不起作用

// 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

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(); 
}; 

有人嗎?

+0

查看wheel事件 - https://developer.mozilla.org/en-US/docs/Web/Events/wheel - DOMMouseScroll是**老** firefox - 鏈接頁面有跨瀏覽器代碼示例 –

+0

我累了替換DOMMouseScroll與輪子仍然不起作用 – Megadevice

+0

你說'不工作'這是沒有意義的...是事件發生了嗎?你至少有你的'mouseWheelHandler'函數嗎? –

回答

0

爲jsc3d最尊敬的,但不是嗅探瀏覽器代理,我會更好地與特徵檢測這一點,像去:

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

編輯: ......並用同樣的邏輯,也去掉勾選了「火狐」:

JSC3D.Viewer.prototype.mouseWheelHandler = function(e) { 
... 
    this.zoomFactor *= (e.deltaY ? e.deltaY : e.wheelDelta ? e.wheelDelta : e.detail) < 0 ? 1.1 : 0.91; 
... 
}; 

那麼,你應該把你的處理程序:

function OnViewerMouseWheeel(canvasX, canvasY, button, depth, mesh) { 
    ... 
} 
viewer.onmousewheel=OnViewerMouseWheeel; 

在最新的FF測試,'onwheel' in document正在恢復真正。

請讓我知道這是否解決了這個問題。

0

if (this.addEventListener) { 
 
\t // IE9, Chrome, Safari, Opera 
 
\t this.addEventListener("mousewheel", MouseWheelHandler, false); 
 
\t // Firefox 
 
\t this.addEventListener("DOMMouseScroll", MouseWheelHandler, false); 
 
} 
 
// IE 6/7/8 
 
else this.attachEvent("onmousewheel", MouseWheelHandler);

Firefox使用的DOMMouseScroll事件,而不是鼠標滾輪

+0

似乎在老版本的Firefox中使用DOMMouseScroll – Megadevice