javascript - 如何在移动设备上触发 Mousedown/Mouseup?

标签 javascript angular

我是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() 函数?

谢谢!

最佳答案

触摸屏的类似事件将是 touchstarttouchend ,它们与桌面的 mousedownmouseup 事件完全相同。来自文档:

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 bubblingevent.stopPropagation()以防止它们冒泡。

如果 mousedown/touchstartmouseup/touchend 事件的逻辑应该相同,那么您可以将多个事件绑定(bind)到监听器,如所述 here .

关于javascript - 如何在移动设备上触发 Mousedown/Mouseup?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51333798/

相关文章:

javascript - SonarQube 5.1.2 版本用户界面设计

Javascript 100% 宽度下拉菜单

javascript - 如何使用循环在 Canvas 中的随机位置多次绘制它?

Angular - 对多个 HTTP 请求进行排序

angular - 如何在 Angular 2+ 应用程序中录制桌面屏幕

c# - 循环遍历响应对象 Angular

php - 如何在 JQuery 或 JavaScript 中使用从 PHP 回显的数据?

javascript - 连续删除数组项(游戏障碍)

angular - 使用 Firebase sdk3 和 Angularfire2 进行身份验证电子邮件/密码

iphone - 在 iPhone Safari 上调试网站