Skip to content

Add disableTouch advanced callback #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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