鼠标事件无法在移动设备中运行

时间:2015-12-04 05:04:37

标签: javascript events mobile three.js touch

我创建了这个360 panorama image,它在桌面上运行良好,但在手机上鼠标事件无效。我该如何解决这个问题呢?

//听众

document.addEventListener("mousedown", onDocumentMouseDown, false);
document.addEventListener("mousemove", onDocumentMouseMove, false);
document.addEventListener("mouseup", onDocumentMouseUp, false);

我将事件更改为

document.addEventListener("touchstart", onDocumentMouseDown, false);
document.addEventListener("touchmove", onDocumentMouseMove, false);
document.addEventListener("touchend", onDocumentMouseUp, false);

但这不适用于移动设备。

2 个答案:

答案 0 :(得分:1)

对于手机,请尝试这样。你必须使用deviceready函数进行启动。

document.addEventListener("deviceready", init, false);

function init() {   
    document.querySelector("#yourbuttonId").addEventListener("touchstart", onDocumentMouseDown, false)
}

此事件对任何应用程序都至关重要。它表明Cordova的设备API已经加载并准备好访问。

Cordova由两个代码库组成:native和JavaScript。加载本机代码时,会显示自定义加载图像。但是,只有在DOM加载后才会加载JavaScript。这意味着在相应的本机代码可用之前,Web应用程序可能会调用Cordova JavaScript函数。

Cordova完全加载后,deviceready事件将触发。一旦事件触发,您就可以安全地调用Cordova API。一旦HTML文档的DOM加载,应用程序通常会附加一个带有document.addEventListener的事件监听器。

deviceready事件的行为与其他事件略有不同。在deviceready事件触发后注册的任何事件处理程序都会立即调用其回调函数。

http://docs.phonegap.com/en/4.0.0/cordova_events_events.md.html#deviceready

阅读此链接。

感谢。

答案 1 :(得分:1)

我找到了答案。对于touch event.clientX和event.clientY无法正常工作,我已将其更改为event.touches [0] .clientX和event.touches [0] .clientY并修复了触摸事件问题。

相关问题