我是creating a website为我的 friend 们让他们玩到更高质量的在线实现Hashiwokakero 。
他们中的一些人有平板电脑,网站上的所有内容都加载得很好,但是当他们触摸并拖动以形成从一个岛到另一个岛的桥梁时,网页会尝试滚动(即使没有可滚动区域) !)。
目前我正在使用以下方法检测鼠标事件:
this.canvas.addEventListener('mousedown',
(mouseEvent) => this.mousePressed(mouseEvent), false);
this.canvas.addEventListener('mouseup',
(mouseEvent) => this.mouseReleased(mouseEvent), false);
是否有一种简单的方法可以在移动设备上调用 mousePressed() 和 mouseReleased() 函数?
谢谢!
最佳答案
触摸屏的类似事件将是 touchstart
和 touchend
,它们与桌面的 mousedown
和 mouseup
事件完全相同。来自文档:
The
touchstart
event is fired when one or more touch points are placed on the touch surface.
和
The
touchend
event is fired when one or more touch points are removed from the touch surface.
您可以查看docs有关触摸事件的更多信息。
我还猜测可能您还需要阻止某些事件冒泡,如果是这样,您可以查看events bubbling和 event.stopPropagation()
以防止它们冒泡。
如果 mousedown
/touchstart
和 mouseup
/touchend
事件的逻辑应该相同,那么您可以将多个事件绑定(bind)到监听器,如所述 here .
关于javascript - 如何在移动设备上触发 Mousedown/Mouseup?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51333798/