Reputation: 599
Is there any way to bind mobile touch events such as touchstart to the jQuery .on() method?
I currently have a drag and drop selection area application I have developed, which detects click and drag movements as so:
$('.foo').on({
mousedown: function(e){
...
}
});
I know you can bind these to handle the events, but it would be useful to even combine the equivalent event (touchstart > mousedown) somehow, would this even be possible just to trigger the mousedown event in a bind for the touchstart event?
Upvotes: 0
Views: 3926
Reputation: 735
I believe this might be what you need. Note that in the mousemove
handler you need to know if the mouse button is down at the moment (can be achieved with setting a boolean variable in start/end event handlers).
function handleEventStart(e){
//something
}
function handleEventMove(e){
//something
}
function handleEventEnd(e){
//something
}
var $foo = $('.foo');
$foo.on('mousedown touchstart', handleEventStart);
$foo.on('mousemove touchmove', handleEventMove);
$foo.on('mouseup touchend touchcancel', handleEventEnd);
Upvotes: 1