jQuery实现获取绑定自定义事件元素的方法
本文实例讲述了jQuery实现获取绑定自定义事件元素的方法。分享给大家供大家参考,具体如下:
(function($){//自定义itemtab事件
$.fn.bind=function(types,data,fn){//重载jQuery.fn.bind方法,用来截获绑定自定义事件的元素
if(typeoftypes=='string'&&'itemtab'==types){
varitemTouchStart=-1;//touchstart位置
varitemTouchMove=-1;//touchend位置,值为-1时表示未触发
varitemTriggerDistance=0;//拖动距离阀值,若大于该值则为拖动列表,若小于等于该值则为点击列表项
varitemMoved=false;//列表是否为拖动状态
$(this).bind('touchstart',function(event){
if(!event.originalEvent.touches.length)returntrue;
itemMoved=false;
itemTouchStart=event.originalEvent.touches[0].pageX;//记录起始位置
}).bind('touchmove',function(event){
if(!event.originalEvent.touches.length)returntrue;
itemTouchMove=event.originalEvent.touches[0].pageX;//当前拖动位置
//console.log('touchmove:',itemTouchStart,itemTouchMove,itemMoved);
if(Math.abs(itemTouchMove-itemTouchStart)>itemTriggerDistance){
itemMoved=true;//列表被拖动
}
}).bind('touchend',function(event){
//console.log('itemMoved:',itemMoved);
if(itemMoved){//列表被拖动过,非点击操作
returntrue;
}
$(this).trigger('itemtab');//触发自定义事件
});
}
returnthis.on(types,null,data,fn);//这种做法具有侵入性,多个类似的代码会相互覆盖,可采用深度复制方式调用原$.fn.bind方法
}
})(jQuery);
希望本文所述对大家jQuery程序设计有所帮助。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短