[英]Timed long press in Javascript within jQuery phonegap app
这里有一个在 Javascript 中长按的好例子: Long Press in JavaScript?
但它不提供知道新闻的持续时间。
如果我想根据印刷机的长度做不同的事情,我不能使用该帖子中的模式。
我试图通过在变量on('mousedown')
保存当前时间然后计算on('mouseup')
的时间差来做类似的事情。 这在“普通”浏览器中的普通 Javasript 页面中工作正常。
然而,在我的 phonegap 应用程序中发生了一些事情,如果手指长时间保持在屏幕上(比如 5 秒 ..),则似乎不会调用mouseup
事件。
这是一些原生移动浏览器行为吗? 我可以以某种方式覆盖它吗?
我使用的是普通的 jQuery 而不是 jQuery mobile。
任何人的想法?
您可以查看一下taphold
和vmouseup
( handleTouchEnd()
第 752 行)事件是如何在jQuery 移动源代码中实现的。
由于它已经过测试和实现,我建议使用 jquery mobile 而不是 jquery 并进行修改(因为它已经处理了与每个移动浏览器相关的所有“怪癖”),并根据需要更改代码。
可以通过查看时间来识别 Click 或 Long Press [jQuery]
function AddButtonEventListener() {
try {
var mousedowntime;
var presstime;
$("button[id$='" + buttonID + "']").mousedown(function() {
var d = new Date();
mousedowntime = d.getTime();
});
$("button[id$='" + buttonID + "']").mouseup(function() {
var d = new Date();
presstime = d.getTime() - mousedowntime;
if (presstime > 999/*You can decide the time*/) {
//Do_Action_Long_Press_Event();
}
else {
//Do_Action_Click_Event();
}
});
}
catch (err) {
alert(err.message);
}
}
请注意,如果您出于某种原因不使用 jQuery Mobile,则此解决方案很有用。 我使用了文章快速触摸事件处理并添加了一段代码
$.event.special.tap = {
distanceThreshold: 10,
timeThreshold: 350,
setup: function () {
var self = this,
$self = $(self);
// Bind touch start
$self.on('touchstart', function (startEvent) {
// Save the target element of the start event
var target = startEvent.target,
touchStart = startEvent.originalEvent.touches[0],
startX = touchStart.pageX,
startY = touchStart.pageY,
threshold = $.event.special.tap.distanceThreshold,
timeout,
expired = false;
function timerFired() {
expired = true;
}
function removeTapHandler() {
clearTimeout(timeout);
$self.off('touchmove', moveHandler).off('touchend', tapHandler).off('touchcancel', removeTapHandler);
};
function tapHandler(endEvent) {
removeTapHandler();
if (target == endEvent.target) {
if (expired) {
$.event.simulate('longtap', self, endEvent);
} else {
$.event.simulate('tap', self, endEvent);
}
}
};
// Remove tap and move handlers if the touch moves too far
function moveHandler(moveEvent) {
var touchMove = moveEvent.originalEvent.touches[0],
moveX = touchMove.pageX,
moveY = touchMove.pageY;
if (Math.abs(moveX - startX) > threshold || Math.abs(moveY - startY) > threshold) {
removeTapHandler();
}
};
// Remove the tap and move handlers if the timeout expires
timeout = setTimeout(timerFired, $.event.special.tap.timeThreshold);
// When a touch starts, bind a touch end and touch move handler
$self.on('touchmove', moveHandler).on('touchend', tapHandler).on('touchcancel', removeTapHandler);
});
}
};
所以,现在我有一个 tap 和 longtap 事件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.