在移動(dòng)端,可以根據(jù)touchStart與touchEnd的間隔來(lái)判斷是點(diǎn)擊,雙擊,還是長(zhǎng)按;
主要的代碼:
let t=0, lastTap=0;
oBtn.addEventListener('touchstart',function(){
t=Date.now();
},false);
oBtn.addEventListener('touchend',function(){
if(Date.now()-t<=700){
console.log('tap');
if(Date.now()-t<=700){
????console.log('tap');
if(Date.now()-lastTap<=300){
????console.log('dbltap');
}
lastTap=Date.now();
}else{
console.log('long tap');
}
lastTap=Date.now();
}
},false);
通過(guò)時(shí)間差來(lái)判斷,當(dāng)然為什么是700, 300,是我的經(jīng)驗(yàn)值,在開(kāi)發(fā)中覺(jué)得這二個(gè)值的界定還是挺合適的。一次雙擊同時(shí)等于二次單擊。當(dāng)然手機(jī)上面雙擊的交互是很少的,長(zhǎng)按還是挺有實(shí)用性的,比如長(zhǎng)按刪除。