找回密码
 注册
搜索
打印 上一主题 下一主题

[JS] HTML5制作WebAPP上下左右拖动,回调函数 jQuery插件

[复制链接]
跳转到指定楼层
楼主
夏穆SUMMUR 发表于 2016-1-4 17:25:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本jQuery插件方法用于感应上下左右的滑动动作并执行对应的回掉函数,常用于HTML5制作WebAPP上下左右拖动。您可以自行建立一个js文件供加载使用(转):

[JavaScript] 纯文本查看 复制代码
/* 1230online.com

用于获取上下左右的拖动事件
$('#box').swipeleft(
	//--回调函数1 - 滑动开始时
	function(n,s){
		n=速度 越小标识越快
		s=步长 滑动的距离
	},
	//--回调函数2 - 滑动结束后
	function(xy){
		xy=x或y的点阵位置 swipeleft swiperight = x   swipetop swipedown = y
	},
	v //--滑动在多少毫秒内才感应 默认300
);
*/
(function($){
	var v=300;//--300以内执行传递参数
	var t=[0,0];
        $.fn.extend({//局部插件,去掉.fn是全局插件
                'swipeleft':function(fn,fn2,_v){//手指左滑动,fn是回调函数
                		v=_v?_v:v;
                        $(this).on('touchstart',function(e){
                                e=e.originalEvent.touches[0];//获取对应触摸对象
                                var sx=0;
                                sx=e.pageX;
                                if(fn2) fn2(sx);
                                t[0]=$(this).transdate();
                                $(this).on('touchend',function(e){
                                		t[1]=$(this).transdate();
                                		var n=t[1]-t[0];
                                        e=e.originalEvent.changedTouches[0];//获取对应触摸对象
                                        var s=sx-e.pageX;
                                        if((v>=n) && s>50){//如果滑动距离大于50px就认为是要触发左滑动事件了
                                                fn(n,s);//调用回调函数
                                        }
                                        $(this).unbind('touchend');
                                });
                        });
                        return this;
                },
                'swiperight':function(fn,fn2,_v){//手指右滑动,fn是回调函数
                		v=_v?_v:v;
                        $(this).on('touchstart',function(e){
                                e=e.originalEvent.touches[0];//获取对应触摸对象
                                var sx=0;
                                sx=e.pageX;
                                if(fn2) fn2(sx);
                                t[0]=$(this).transdate();
                                $(this).on('touchend',function(e){
                                		t[1]=$(this).transdate();
                                		var n=t[1]-t[0];
                                        e=e.originalEvent.changedTouches[0];//获取对应触摸对象
                                        var s=e.pageX-sx;
                                        if((v>=n) && s>50){//如果滑动距离大于50px就认为是要触发右滑动事件了
                                                fn(n,s);//调用回调函数
                                        }
                                        $(this).unbind('touchend');
                                });
                        });
                },
                'swipetop':function(fn,fn2,_v){//手指上滑动,fn是回调函数
                		v=_v?_v:v;
                        $(this).on('touchstart',function(e){
                                e=e.originalEvent.touches[0];//获取对应触摸对象
                                var sy=0;
                                sy=e.pageY;
                                if(fn2) fn2(sy);
                                t[0]=$(this).transdate();
                                $(this).on('touchend',function(e){
                                		t[1]=$(this).transdate();
                                		var n=t[1]-t[0];
                                        e=e.originalEvent.changedTouches[0];//获取对应触摸对象
                                        var s=sy-e.pageY;
                                        if((v>=n) && s>50){//如果滑动距离大于50px就认为是要触发上滑动事件了
                                                fn(n,s);//调用回调函数
                                        }
                                        $(this).unbind('touchend');
                                });
                        });
                },
                'swipedown':function(fn,fn2,_v){//手指下滑动,fn是回调函数
                		v=_v?_v:v;
                        $(this).on('touchstart',function(e){
                                e=e.originalEvent.touches[0];//获取对应触摸对象
                                var sy=0;
                                sy=e.pageY;
                                if(fn2) fn2(sy);
                                t[0]=$(this).transdate();
                                $(this).on('touchend',function(e){
                                		t[1]=$(this).transdate();
                                		var n=t[1]-t[0];
                                        e=e.originalEvent.changedTouches[0];//获取对应触摸对象
                                        var s=e.pageY-sy;
                                        if((v>=n) && s>50){//如果滑动距离大于50px就认为是要触发下滑动事件了
                                                fn(n,s);//调用回调函数
                                        }
                                        $(this).unbind('touchend');
                                });
                        });
                },
                'transdate': function (){return Number(new Date().getTime());}
        });
})(jQuery);




夏穆中文网|SUMMUR.COM
快速回复 返回顶部 返回列表