Reputation: 3913
I have a function that gets called for touchmove
and I need to find out the direction of the mouse movement. Requested values are "left" and "right". How can I do this without using any plugins??
document.addEventListener('touchmove',
function(event, direction, distance, duration, fingerCount) {
if(direction == 'left'){
my.MouseWheel.handle(-1);
} else if (direction == 'right'){
my.MouseWheel.handle(1);
}
});
Upvotes: 1
Views: 1581
Reputation: 620
Use the touchstart event, then get the coordinates of the touch using pageX and pageY. then on the touchmove event get the coordinates again and compare to the previous to find the direction.
var mouse_x;
var mouse_y;
function load(){
document.addEventListener('touchstart',get_mouse_coords,false);
document.addEventListener('touchmove',check_swipe,false);
}
function get_mouse_coords(e){
mouse_x = e.pageX;
mouse_y = e.pageY;
}
function check_swipe(e){
var new_mouse_x = e.pageX;
if ((new_mouse_x - mouse_x) > 75){
//swiped right
}else if ((new_mouse_x - mouse_x) < -75){
//swiped left
}
}
Upvotes: 1