jQuery Touchwipe - disable default scrolling for 1 axis only

前端 未结 1 1607
耶瑟儿~
耶瑟儿~ 2021-02-10 11:09

Im using the jQuery Touchwipe plugin: http://www.netcu.de/jquery-touchwipe-iphone-ipad-library

With preventDefaultEvents: true its possible to dissable the default dragg

相关标签:
1条回答
  • 2021-02-10 11:46

    I had the same need and extended the touchwipe plugin to pass the event to the wipeLeft, wipeRight, wipeUp and wipeDown callbacks. That allows me to set preventDefaultEvents: false in config and then in my specific callbacks if needed, do first thing: e.preventDefault();.

    I sent modifications to plugin author.

    The modified plugin:

    (function($) {
    $.fn.touchwipe = function(settings) {
        var config = {
                min_move_x: 20,
                min_move_y: 20,
                wipeLeft: function(e) { },
                wipeRight: function(e) { },
                wipeUp: function(e) { },
                wipeDown: function(e) { },
                preventDefaultEvents: true
        };
    
        if (settings) $.extend(config, settings);
    
        this.each(function() {
            var startX;
            var startY;
            var isMoving = false;
    
            function cancelTouch() {
                this.removeEventListener('touchmove', onTouchMove);
                startX = null;
                isMoving = false;
            }
    
            function onTouchMove(e) {
                if(config.preventDefaultEvents) {
                    e.preventDefault();
                }
                if(isMoving) {
                    var x = e.touches[0].pageX;
                    var y = e.touches[0].pageY;
                    var dx = startX - x;
                    var dy = startY - y;
                    if(Math.abs(dx) >= config.min_move_x) {
                        cancelTouch();
                        if(dx > 0) {
                            config.wipeLeft(e);
                        }
                        else {
                            config.wipeRight(e);
                        }
                    }
                    else if(Math.abs(dy) >= config.min_move_y) {
                            cancelTouch();
                            if(dy > 0) {
                                config.wipeDown(e);
                            }
                            else {
                                config.wipeUp(e);
                            }
                        }
                }
            }
    
            function onTouchStart(e)
            {
                if (e.touches.length == 1) {
                    startX = e.touches[0].pageX;
                    startY = e.touches[0].pageY;
                    isMoving = true;
                    this.addEventListener('touchmove', onTouchMove, false);
                }
            }
            if ('ontouchstart' in document.documentElement) {
                this.addEventListener('touchstart', onTouchStart, false);
            }
        });
    
        return this;
    };
    
    })(jQuery);
    
    0 讨论(0)
提交回复
热议问题