zepto
zepto copied to clipboard
Trigger touch events with touch coords
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});
Useful, thanks. Could you show a code sample to use the event coordinates as variables?
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);
});
We'll possibly support this in the future, but you could just use two elements, overlay them and check for touch events separately.