滑动事件触发两次

时间:2018-11-04 00:04:16

标签: javascript jquery swipe

我从某个地方(不记得在哪里)修改了此设置,以包含一些额外的滑动触发器(例如,从右滑动)。

它工作正常,但是所有定义的滑动事件(例如swipeleftswipefromright等)在一次滑动中都会触发两次。

请原谅部分缩小的代码。

//swipeall
(function()
{
    var supportTouch='ontouchend' in document,scrollEvent="touchmove scroll",
        touchStartEvent=supportTouch?"touchstart mousedown":"mousedown",
        touchStopEvent=supportTouch?"touchend mouseup":"mouseup",
        touchMoveEvent=supportTouch?"touchmove mousemove":"mousemove";
    $.event.special.swipeall=
    {
        setup:function()
        {
            var thisObject=this;
            var $this=$(thisObject);
            $this.bind(touchStartEvent,function(event)
            {
                var data='originalEvent' in event && 'touches' in event.originalEvent && event.originalEvent.touches 
                    ? event.originalEvent.touches[0]
                    :('touches' in event && event.touches
                        ?event.touches[0]
                        :event
                    ),
                    start=
                    {
                        time:(new Date()).getTime(),
                        coords:[data.pageX,data.pageY],
                        origin:$(event.target)
                    },
                    stop;
                    function moveHandler(event)
                    {
                        if(!start){return;}
                        var data=event.originalEvent.touches
                                ?event.originalEvent.touches[0]
                                :event;
                            stop={time:(new Date()).getTime(),
                            coords:[data.pageX,data.pageY]
                    };
            }
            $this.bind(touchMoveEvent,moveHandler)
                .one(touchStopEvent,function(event)
                {
                    $this.unbind(touchMoveEvent,moveHandler);
                    if(start&&stop)
                    {
                        if(stop.time-start.time<1000&&Math.abs(start.coords[1]-stop.coords[1])>30&&Math.abs(start.coords[0]-stop.coords[0])<75)
                        {
                            if(start.coords[1]>stop.coords[1])
                            {                                    
                                if(start.coords[1] >= $(window).height()-25)
                                {
                                    start.origin
                                        .trigger("swipeall")
                                        .trigger("swipeup")
                                        .trigger("swipefrombottom");    
                                } 
                                else
                                {
                                     start.origin
                                        .trigger("swipeall")
                                        .trigger("swipeup");    
                                }
                            }
                            else
                            {
                                if(start.coords[1] <= 25) 
                                {
                                    start.origin
                                        .trigger("swipeall")
                                        .trigger("swipedown")
                                        .trigger("swipefromtop");
                                }
                                else
                                {
                                    start.origin
                                        .trigger("swipeall")
                                        .trigger("swipedown");
                                }
                            }
                        }
                        else if(stop.time-start.time<1000&&Math.abs(start.coords[0]-stop.coords[0])>30&&Math.abs(start.coords[1]-stop.coords[1])<75)
                        {
                            if(start.coords[0]>stop.coords[0])
                            {
                                if(start.coords[0]>=$(window).width()-25) start.origin.trigger("swipeall").trigger("swipeleft").trigger("swipefromright")
                                else start.origin.trigger("swipeall").trigger("swipeleft");
                            }
                            else
                            {
                                if(start.coords[0]<=25) start.origin.trigger("swipeall").trigger("swiperight").trigger("swipefromleft");
                                else start.origin.trigger("swipeall").trigger("swiperight");
                            }
                        }
                    }
                    start=stop=undefined;
                });
            });
        }
    };
    $.each({swipedown:"swipeall",swipeup:"swipeall",swiperight:"swipeall",swipeleft:"swipeall",swipefromtop:"swipeall",swipefrombottom:"swipeall",swipefromleft:"swipeall",swipefromright:"swipeall"},function(event,sourceEvent)
    {
        $.event.special[event]=
        {
            setup:function() 
            {            
                 $(this).bind(sourceEvent,$.noop);
            }
         };
         $.fn[event]=function(fn)
         {
             return fn?this.bind(event,fn):this.trigger(event);
         };
         if($.attrFn)
         {
              $.attrFn[event]=true;
         }
    });
})();

要复制:

var count = 0;
$('.elem').swiperight(function()
{ 
    count++;
    console.log(count);
});

打印:

`1`
`2`

0 个答案:

没有答案