Skip to content

Conversation

@nyroDev
Copy link

@nyroDev nyroDev commented Nov 27, 2015

Add an advanced callback to let the developper disable touch event when some condtions applied.

In my case, I had a swipeleft/swiperight inside a scrolled content.
It was not working properly because the events was stopped.

Using jQuery mobile, I implemented it this way:

var touchStartAmplifier = false,
    touchAmplifierStoped = false;

$elm.mCustomScrollbar({
    /* ... */
    advanced: {
        disableTouch: function(e, coords) {
            if (e.type == 'touchstart') {
                if ($(e.target).closest('#SWIPE-CONTAINER').length > 0) {
                    touchStartAmplifier = coords;
                } else {
                    touchStartAmplifier = false;
                }
            } else if (e.type == 'touchmove' && touchStartAmplifier) {
                // already stoped
                if (touchAmplifierStoped)
                    return true;

                var diffX = Math.abs(touchStartAmplifier[1] - coords[1]),
                    diffY = Math.abs(touchStartAmplifier[0] - coords[0]);

                // Not enough drag yet to decide, stop it
                if (diffX < $.event.special.swipe.horizontalDistanceThreshold && diffY < $.event.special.swipe.verticalDistanceThreshold) {
                    return true;
                }

                if (diffX > diffY) {
                    touchAmplifierStoped = true;
                    return true;
                } else {
                    touchStartAmplifier = false;
                }

            } else if (e.type == 'touchend' && touchStartAmplifier) {
                touchStartAmplifier = false;
                if (touchAmplifierStoped) {
                    touchAmplifierStoped = false;
                    return true;
                }
            }
        }
    }
});

With this, touch work as before on every other elements.
When touch start on '#SWIPE-CONTAINER, it will wait until it can decide wether it's an horizontal or vertical gesture.
If horizontal, all other gestures will be stopped.
If vertical, all other gestures will pass through.

The result is only a small gap when the gesture has to pass through, which is acceptable in my case.

I tried to add the doc too, but I'm not sure if it will work correctly and fit your requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant