'Differentiate Touch and Swap Event in Javascript

I have a image gallery which I used for swaping images. In gallery I used touchstart, touchmove, touchend and touchcancel events. But now I need to handle the touch event as well. So, I do I differentiate the touch and swap event separately on mobile devices?



Solution 1:[1]

I'm using this trick:

$("#some_element")
        .on('touchstart', function(e) {
            touch_y = e.changedTouches[event.changedTouches.length - 1].clientY;
            touch_x = e.changedTouches[event.changedTouches.length - 1].clientX;
        })
        .on("click touchend", function (e) {
            if(e.type=='touchend' && (Math.abs(touch_y-e.changedTouches[event.changedTouches.length - 1].clientY)>3 || Math.abs(touch_x-e.changedTouches[event.changedTouches.length - 1].clientX)>3)) return;
            // Your code here
            alert('This element only touched, not swiped');
        });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1