使用jQuery在Android上确定长点击(长按,点击保持)
我已经能够使用jQuery和HTML页面在Android上处理touchstart、touchmove和touchend事件。现在我想看看决定一个长点击事件的诀窍是什么,其中一个点击并保持3秒钟。我似乎还没弄明白。我只想在jQuery中实现这一点,而不需要senchatouch、JQTouch、jQMobile等使用jQuery在Android上确定长点击(长按,点击保持),jquery,android,tapandhold,Jquery,Android,Tapandhold,我已经能够使用jQuery和HTML页面在Android上处理touchstart、touchmove和touchend事件。现在我想看看决定一个长点击事件的诀窍是什么,其中一个点击并保持3秒钟。我似乎还没弄明白。我只想在jQuery中实现这一点,而不需要senchatouch、JQTouch、jQMobile等 我喜欢jQTouch的概念,尽管它并没有给我提供太多的东西,而且我的一些代码也因此中断。使用Sencha Touch,我不喜欢从jQuery转移到Ext.js和一些新的Javascri
我喜欢jQTouch的概念,尽管它并没有给我提供太多的东西,而且我的一些代码也因此中断。使用Sencha Touch,我不喜欢从jQuery转移到Ext.js和一些新的Javascript抽象方式,尤其是当jQuery如此强大的时候。所以,我想单独用jQuery来解决这个问题。我已经能够使用jQuery自己做很多jQTouch和Sencha触摸的事情。而且jQMobile仍然是测试版,还没有足够的定向到Android上。定时器没有使用,但只有在用户长时间点击后释放手指后才会触发
var startTime, endTime;
var gbMove = false;
window.addEventListener('touchstart',function(event) {
startTime = new Date().getTime();
gbMove = false;
},false);
window.addEventListener('touchmove',function(event) {
gbMove = true;
},false);
window.addEventListener('touchend',function(event) {
endTime = new Date().getTime();
if(!gbMove && (endTime-startTime)/1000 > 2)
alert('tap hold event');
},false);
为什么不使用js定时器呢?我做了这样的事情:
i=0;
$drink = document.getElementById('drink');
$drink.addEventListener('touchstart',function(event){
$('#drink .mainBtnText').text('HOLD'); //mostly for debugging
t = window.setInterval(function(event){
i+=.5;
if (i>=1){
alert('taphold');
window.clearInterval(t);
i=0;
}
},500);
});
$drink.addEventListener('touchend',function(event){
$('#drink .mainBtnText').text('Drink');
i=0;
window.clearInterval(t);
});
到目前为止,我还没有遇到任何问题……诚然,我还没有做过非常广泛的设备测试,但它在我的桌面(使用mousedown/mouseup)以及HTC Droid Eris(安卓1.6)和Droid RAZR(安卓2.3)上都能正常工作。我做了这样的事情:
var touchCounter;
window.addEventListener('touchstart', function () {
var count = 0;
touchCounter = setInterval(function () {
count++;
if (count == 10) alert('touch lasted 10 seconds');
}, 1000);
});
window.addEventListener('touchend', function () {
clearInterval(touchCounter);
touchCounter = null;
});
虽然不是jQuery,但我在Android应用程序中是这样做的:
var touchStartTimeStamp = 0;
var touchEndTimeStamp = 0;
window.addEventListener('touchstart', onTouchStart,false);
window.addEventListener('touchend', onTouchEnd,false);
var timer;
function onTouchStart(e) {
touchStartTimeStamp = e.timeStamp;
}
function onTouchEnd(e) {
touchEndTimeStamp = e.timeStamp;
console.log(touchEndTimeStamp - touchStartTimeStamp);// in miliseconds
}
我希望这会有所帮助。它在触摸移动/开始或结束于不同位置时表现正常,奇怪的是。不太清楚为什么会这样,但我也没有抱怨:P
var timer;
function onTouchStart(e) {
touchStartTimeStamp = e.timeStamp;
}
function onTouchEnd(e) {
touchEndTimeStamp = e.timeStamp;
console.log(touchEndTimeStamp - touchStartTimeStamp);// in miliseconds
}