Serhio g. Lazin Serhio g. Lazin - 6 months ago 31
iOS Question

Prevent vertical touchmove event, but leave horizontal touch and scroll opportunity via buttons

I have an iPad app with jQuery widget. Widget can be scrolled horizontally and vertically. I need to prevent vertical scroll via touch event, but leave opportunity to scroll via buttons (like slider).

Needs just remove user vertical touch, but leave horizontal touch. Ideas?

Answer
mainContainer.on('touchmove', function(e) {

    var xStart, yStart = 0;

    xStart = e.touches[0].screenX;
    yStart = e.touches[0].screenY;

    var xMovement = Math.abs(e.touches[0].screenX - xStart);
    var yMovement = Math.abs(e.touches[0].screenY - yStart);
    if (friendsList.has($(e.target)).length
        || msgContainer.has($(e.target)).length) {

        if ((yMovement * 3) > xMovement) {
            e.preventDefault();
        }

    }

});

This code inside 'touchmove' event prevents vertical scroll by user touching, but allows scroll as it is.