首页 > 代码库 > 手机的touch事件(基于jquery)
手机的touch事件(基于jquery)
javascript代码:
$.swipe=function(opt){
var o = $.extend({
mainSelector:"",
swipeLeft:function(e,v){},
swipeRight:function(e,v){},
swipeEnds:function(v,e){}
}, opt || {});
var startX=0,startY=0;
var obj={
touchStart: function (e){
var touchx= e.touches[0].pageX;
var touchy= e.touches[0].pageY;
startX = parseInt(touchx,10);
startY = parseInt(touchy,10);
},
touchMove:function(e){
var touchmx= e.touches[0].pageX,
touchmy= e.touches[0].pageY,
dirX=parseInt(touchmx-startX,10);
dirY=parseInt(touchmy-startY,10);
if(dirX<0){
o.swipeLeft(e,dirX);
}else{
o.swipeRight(e,dirX);
}
if(dirY<0){
o.swipeTop(e,dirY);
}else{
o.swipeBottom(e,dirY);
}
},
touchEnd:function(e){
o.swipeEnds(e);
},
bindEvent:function(els){
var elem=$(els);
for(var i=0,_len=elem.length;i<_len;i++){
elem[i].addEventListener(‘touchstart‘, obj.touchStart, false);
elem[i].addEventListener(‘touchmove‘, obj.touchMove, false);
elem[i].addEventListener(‘touchend‘, obj.touchEnd, false);
}
}
};
obj.bindEvent(o.mainSelector);
return obj;
}; var o = $.extend({
mainSelector:"",
swipeLeft:function(e,v){},
swipeRight:function(e,v){},
swipeEnds:function(v,e){}
}, opt || {});
var startX=0,startY=0;
var obj={
touchStart: function (e){
var touchx= e.touches[0].pageX;
var touchy= e.touches[0].pageY;
startX = parseInt(touchx,10);
startY = parseInt(touchy,10);
},
touchMove:function(e){
var touchmx= e.touches[0].pageX,
touchmy= e.touches[0].pageY,
dirX=parseInt(touchmx-startX,10);
dirY=parseInt(touchmy-startY,10);
if(dirX<0){
o.swipeLeft(e,dirX);
}else{
o.swipeRight(e,dirX);
}
if(dirY<0){
o.swipeTop(e,dirY);
}else{
o.swipeBottom(e,dirY);
}
},
touchEnd:function(e){
o.swipeEnds(e);
},
bindEvent:function(els){
var elem=$(els);
for(var i=0,_len=elem.length;i<_len;i++){
elem[i].addEventListener(‘touchstart‘, obj.touchStart, false);
elem[i].addEventListener(‘touchmove‘, obj.touchMove, false);
elem[i].addEventListener(‘touchend‘, obj.touchEnd, false);
}
}
};
obj.bindEvent(o.mainSelector);
return obj;
传入的是选择器和swipeLeft,swipeRight的回调事件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。