prevent touchstart when page scrolling on mobile

6.8k views Asked by At

I would like to disable the touchstart event when a user scrolls down the page of their mobile device. The page has various elements which when you click toggles a class but I want thos touchstart event diabled when the user swipes down to scroll down the page.

JQUERY

$(document).on('touchstart click', '.flip-container ', function(event){                       
         event.preventDefault();
        $(this).find('.flipper').toggleClass('hover');
}); 

Anyone any idea how? Thanks!

3

There are 3 answers

0
Jonas Grumann On

I think you could use a flag when the page is scrolling and add the toggle that class only when it is not scrolling. Something like:

//SET THE FLAG
var scrolling = false;
var endScrolling;

$(window).on("scroll", function() {
    scrolling = true;
    endScrolling = window.setTimeout(function() {
        scrolling = false;
        window.clearTimeout(endScrolling);
    }, 20);
});


$(document).on('touchstart click', '.flip-container ', function(event){                       
     event.preventDefault();
    //BLOCK THE CLASS TOGGLE IF THE PAGE IS SCROLLING
    if(!scrolling) {
        $(this).find('.flipper').toggleClass('hover');
    }
}); 
0
Levarne Sobotker On
var touchmoved;
$('button').on('touchend', function(e){
    if(touchmoved != true){
        // you're on button click action
    }
}).on('touchmove', function(e){
    touchmoved = true;
}).on('touchstart', function(){
    touchmoved = false;
});

https://stackoverflow.com/a/32120668/3678996

0
Lam Chang On

You can try something like this:

var initialValue;
var finalValue;

$( 'body' ).on( 'touchstart', 'yourSelectorHere', function() {
    initialValue = $( this ).offset().top;
}).on( 'touchend', 'yourSelectorHere', function() {
    finalValue = $( this ).offset().top;
});

$( 'body' ).on( 'touchend', 'yourSelectorHere', function(e) {
    setTimeout( function(){ // is time for get the finalValue
        if( initialValue == finalValue ){
            // your code here
        }
    }, 300);
});