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程序设计有所帮助。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短