webapp上下左右拖动,回调函数 jQuery插件,改造版本

webapp上下左右拖动,回调函数 jQuery插件,改造版本

Tony哥
2015-09-15 / 0 评论 / 137 阅读 / 正在检测是否收录...

本jQuery插件方法用于感应上下左右的滑动动作并执行对应的回掉函数,您可以自行建立一个js文件供加载使用

/*
用于获取上下左右的拖动事件
$('#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);
0

评论 (0)

取消