How to swipe top down JQuery mobile

后端 未结 2 1990
不思量自难忘°
不思量自难忘° 2020-11-28 05:44

I\'m trying to make event on swiping up and down instead of left and right

i have this roll as image shows: \"h

2条回答
  •  Happy的楠姐
    2020-11-28 06:20

    I had Problems with the accepted answer here, because swiping wasn't detected when origin and target of the swipe were not the same.

    Here maybe an easier answer, where i directly override the jquery handleSwipe event (based on jquery.mobile-1.4.5) and append it with a vertical swipe, called up and down:

    (function( $, window, undefined ) {
    
        //custom handleSwipe with swiperight, swipeleft, swipeup, swipedown
        $.event.special.swipe.handleSwipe = function( start, stop, thisObject, origTarget ) {
            if ( stop.time - start.time < $.event.special.swipe.durationThreshold ) {
                var horSwipe = Math.abs( start.coords[0] - stop.coords[0] ) > $.event.special.swipe.horizontalDistanceThreshold;
                var verSwipe = Math.abs( start.coords[1] - stop.coords[1] ) > $.event.special.swipe.verticalDistanceThreshold;
                if( horSwipe != verSwipe ) {
                    var direction;
                    if(horSwipe)
                        direction = start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight";
                    else
                        direction = start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown";
                    $.event.trigger($.Event( "swipe", { target: origTarget, swipestart: start, swipestop: stop }), undefined, thisObject);
                    $.event.trigger($.Event( direction, { target: origTarget, swipestart: start, swipestop: stop }), undefined, thisObject);
                    return true;
                }
                return false;
            }
            return false;
        }
    
        //do binding
        $.each({
            swipeup: "swipe.up",
            swipedown: "swipe.down"
        }, function( event, sourceEvent ) {
            $.event.special[ event ] = {
                setup: function() {
                    $( this ).bind( sourceEvent, $.noop );
                },
                teardown: function() {
                    $( this ).unbind( sourceEvent );
                }
            };
        }); 
    })( jQuery, this );
    

提交回复
热议问题