本文详解移动端JavaScript事件模型,包括TOUCH与GESTURE事件,深入解析click事件的300ms延迟成因及FastClick解决方案。同时对比分析Touch.js、Hammer.js与Zepto.js等常用手势库,通过代码实例展示单击、双击、滑动等手势的实现逻辑,帮助开发者提升移动端交互体验。
移动端事件基础模型
一、事件基础
PC端常见的事件类型包括:click(单击)、mouseover、mouseout、mouseenter、mouseleave、mousemove、mousedown、mouseup、mousewheel、keydown、keyup、load、scroll、blur、focus、change等。
移动端则主要涉及:click(单击)、load、scroll、blur、focus、change,以及input事件(用于替代keyup和keydown)。此外,移动端还引入了TOUCH事件模型以处理单手指操作,以及GESTURE事件模型以处理多手指操作。
TOUCH事件模型包含以下核心事件:touchstart、touchmove、touchend、touchcancel。
GESTURE事件模型包含以下核心事件:gesturestart、gesturechange、gestureend。
在移动端,click事件特指单击操作。在实际开发中,通常需要区分单击和双击的行为,因此移动浏览器在识别click时,必须确认用户仅执行了一次点击操作,才会触发该事件。
在移动端直接使用click事件会存在300ms的延迟。这是因为浏览器在接收到第一次点击后,需要等待300ms以判断用户是否进行第二次点击。如果在此期间触发了第二次点击,则判定为双击,不触发click;若未触发第二次点击,才判定为单击并执行click事件。
以下代码展示了如何在移动端手动模拟click事件的行为。
function on(curEle,type,fn){
curEle.addEventListener(type,fn,false);
}
var oBox = document.querySelector('.box');
//移动端采用click存在300ms延迟
// oBox.addEventListener('click',function(){
// this.style.webkitTransform = 'rotate(360deg)'
// },false)
//使用TOUCH事件模型实现点击操作(单击&&双击)
on(oBox,'touchstart',function(ev){
//ev:TouchEvent事件 属性 type、target、preventDefault(returnValue)、stopPropagation、changedTouches、touches
//changedTouches和touches都是手指信息的集合(touchList),touches获取到值的必要条件只有手指还在屏幕上才可以获取,所以在touchend事件中如果想获取手指离开的瞬间坐标只能使用changedTouches获取
var point = ev.touches[0];
this['strX'] = point.clientX;
this['strY'] = point.clientY;
this['isMove'] = false;
})
on(oBox,'touchmove',function(ev){
var point = ev.touches[0];
var newX = point.clientX,
newY = point.clientY;
//判断是否发生滑动,我们需要判断偏移的值是否在30PX以内
if(Math.abs(newX-this['strX'])>30 || Math.abs(newY-this['strY'])>30){
this['isMove'] = true;
}
})
on(oBox,'touchend',function(ev){
if(this['isMove'] === false){
//没有发生移动 点击
this.style.webkitTransitionDuration = '1s';
this.style.webkitTransform = 'rotate(360deg)';
var delayTimer = window.setTimeout(function(){
this.style.webkitTransitionDuration = '0s';
this.style.webkitTransform = 'rotate(0deg)';
}.bind(this),1000);
}else{
//滑动
this.style.background = 'red';
}
})
此外,也可以引入fastclick.js库来解决移动端click事件的300ms延迟问题(GitHub地址:https://github.com/zhouxiaotian/fastclick)。
移动端手势识别逻辑
二、常用手势与事件类型
涉及单击和双击操作,通常基于300ms的时间窗口进行判断。
涉及点击和长按操作,通常基于750ms的时间窗口进行判断。
涉及点击和滑动操作,判断依据是X/Y轴的偏移距离是否在30PX以内。若超过30PX,则判定为滑动。
区分左右滑动和上下滑动:若X轴偏移距离大于Y轴偏移距离,则判定为左右滑动;反之则为上下滑动。
区分左滑和右滑:若偏移距离大于0,则判定为右滑;反之则为左滑。
常用移动端事件库对比
二、常用的事件库
FastClick.js:主要用于解决CLICK事件存在的300ms延迟问题。
TOUCH.js:百度推出的移动端手势库,GitHub地址:https://github.com/Clouda-team/touch.code.baidu.com。
以下代码展示了TOUCH.js的使用实例。
var oBox = document.querySelector('.box');
//单击
touch.on(oBox,'tap',function(ev){
this.style.webkitTransitionDuration = '1s';
this.style.webkitTransform = 'rotate(360deg)';
var delayTimer = window.setTimeout(function(){
this.style.webkitTransitionDuration = '0s';
this.style.webkitTransform = 'rotate(0deg)';
window.clearTimeout(delayTimer)
}.bind(this),1000)
})
//双击
touch.on(oBox,'doubletap',function(ev){
this.style.webkitTransitionDuration = '1s';
this.style.webkitTransform = 'rotate(-360deg)';
var delayTimer = window.setTimeout(function(){
this.style.webkitTransitionDuration = '0s';
this.style.webkitTransform = 'rotate(0deg)';
window.clearTimeout(delayTimer)
}.bind(this),1000)
})
//长按
touch.on(oBox,'hold',function(ev){
this.style.backgroundColor = 'red';
})
Hammer.js:另一款常用的移动端手势库。
Zepto.js:被誉为移动端的轻量级jQuery。由于jQuery主要面向PC端,其代码中包含大量针对低版本IE浏览器的兼容处理;而Zepto专注于移动端开发,因此未对低版本IE提供支持。
jQuery提供了丰富的选择器类型及DOM操作方法,而Zepto仅实现了部分常用功能。例如,jQuery中的动画方法包括animate、hide、show、fadeIn、fadeOut、fadeToggle、slideDown、slideUp、slideToggle等,但在Zepto中仅保留了animate方法。
Zepto的源代码体积比jQuery小很多。
Zepto专为移动端开发而生,因此相较于jQuery更适合移动端项目。
Zepto的animate动画方法支持CSS3动画操作。
Zepto专门提供了移动端常用的事件操作接口,包括:tap、singleTap、doubleTap、longTap、swipe、swipeUp、swipeDown、swipeLeft、swipeRight。
以下代码展示了Zepto.js的使用实例。
$('.box').singleTap(function(ev){
$(this).animate({
rotate:'360deg'
},1000,'linear',function(){
this.style.webkitTransform = 'rotate(0)'
})
})
$('.box').on('touchstart',function(){
$(this).css('background','red')
})
$.ajax({
url:'',
type:'get',
dataType:'json',
cache:false,
success:function(){
}
})
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。