【发布时间】:2018-04-04 11:55:45
【问题描述】:
我正在 Ionic 3 中寻找一个单独的事件处理程序,用于开始和结束对移动应用程序上的 HTML 元素的触摸。
我发现了许多相关且已解决的问题,但 Ionic 3 没有一个,它目前似乎只支持“点击、按下、平移、滑动、旋转和捏合”(https://ionicframework.com/docs/components/#gestures)。 而且这些似乎都没有在开始时提供“处理程序”,而只是在结束时提供。我看到他们确实提供了触摸持续时间(deltaTime)的数据,但到那时它对我的目的没有用处。
对于更多上下文,我想要的是在屏幕第一次触摸元素时清除相关超时,然后查看对同一特定元素的触摸是否在特定时间内结束(例如250 ms,因此可以将其评估为“点击”)。
例如这样的:
JS:
timeout_1 = setTimeout(function() {
// do something if timeout_1 not cleared by touch start
}, 4000);
touched(event) {
clearTimeout(timeout_1);
touching_x = true
timeout_2 = setTimeout(function() {
touching_x = false
// store event details and do other things if timeout_2 not cleared by touch end
}, 250);
}
touch_ended(event) {
if (touching_x==true) {
clearTimeout(timeout_2);
// store event details and do some other things
}
}
HTML:
<button ion-button type="button" (button_touch_started) = "touched($event)" (button_touch_ended) = "touch_ended($event)">Touch button</button>
高精度(低至毫秒)非常重要,尤其是对于触摸开始时间。
感谢任何建议。
【问题讨论】:
-
是否需要touchstart和touchend事件。如果你需要这个你需要哪个元素
-
如上所述,我需要开始和结束。它可以是任何常规的 HTML 元素,例如一个按钮,在这种情况下它看起来像这样:
<button ion-button type="button" (button_touch_started)="touched()">Touch this button</button>(或者如果它是一个<div>会有所不同吗?) -
我会在答案中放一个代码示例,请检查
标签: html ionic3 touch dom-events touchstart