2

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!

user1937021
  • 10,151
  • 22
  • 81
  • 143
  • Does this answer your question? [prevent touchstart when swiping](https://stackoverflow.com/questions/7069458/prevent-touchstart-when-swiping) – showdev Jul 01 '21 at 04:03

3 Answers3

7
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

Levarne Sobotker
  • 1,140
  • 12
  • 12
1

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');
    }
}); 
Jonas Grumann
  • 10,438
  • 2
  • 22
  • 40
0

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);
});
Lam Chang
  • 11
  • 1