zepto icon indicating copy to clipboard operation
zepto copied to clipboard

Trigger touch events with touch coords

Open apoleshchuk opened this issue 14 years ago • 3 comments

Sometimes need touchstart and touchend event coordinates in triggered event (tap, swipe, etc.).

Simple solutions – send touch object as data param when trigger target event:

$(touch.target).trigger('tap', {touch: touch});

apoleshchuk avatar Sep 08 '11 13:09 apoleshchuk

Useful, thanks. Could you show a code sample to use the event coordinates as variables?

yumyo avatar Nov 08 '11 11:11 yumyo

var layout = $(document.body),
    hotZoneSize = 0.25;

function getHotZone(x, y) {
        var o = layout.offset();

        if (x < o.left + o.width * hotZoneSize)
            return 'left';
        if (x > o.left + o.width * (1 - hotZoneSize))
            return 'right';

        return null;
    }
}
$(document.body).bind('tap swipeLeft swipeRight', function(event) {
    var touch = event.data.touch,
        // left or right hot zone on screen
        hotZone = getHotZone(touch.x2 || touch.x1 || 0);
});

apoleshchuk avatar Nov 09 '11 06:11 apoleshchuk

We'll possibly support this in the future, but you could just use two elements, overlay them and check for touch events separately.

madrobby avatar Sep 04 '13 19:09 madrobby