Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to detect when a user swipes left or right.

ANSWERED

Is there as why to bind or detect when the user has swiped left or right on the screen in JavaScript?

I have searched for the documentation it's either not in there or I totally missed it. 

 

I did look at the panorama component and it's not going to work for what I need. 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

What i use in My app is pretty simple but it works

 


var y = 0;
var x=0;
var timerText = document.getElementById("timer");
timerText.onmousedown = function(evt) {
  y = evt.screenY;
  x = evt.screenX;
}
timerText.onmouseup = function(evt) {
  let yMove = evt.screenY-y;
  let xMove = evt.screenX-x;
  if (yMove< -60) {//swipe up  };

  if (yMove> 60) {//swipe down};

  if (xMove< -60) {//swipe left  };

  if (xMove> 60) {//swipe right};


 
}

View best answer in original post

Best Answer
10 REPLIES 10

You don't have an event for swipe, but I think you can read the x position for mouse down and x position for mouse up and 'guess' the swipe direction. I would do something like

let myButton = document.getElementById("myButton");

myButton.onmousedown = function(evt) {
  console.log("Before swipe x position: " + evt.screenX);
}
myButton.onmouseup = function(evt) { console.log("After swipe x position: " + evt.screenX); }

 

 

Best Answer
0 Votes

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Best Answer
0 Votes
You don't have an event for swipe, but I think you can read the x position for mouse down and x position for mouse up and 'guess' the swipe direction.
I would do something like :

 

let myButton = document.getElementById("myButton");

myButton.onmousedown = function(evt) {
  console.log("Before swipe x position: " + evt.screenX);
}
myButton.onmouseup = function(evt) {
  console.log("After swipe x position: " + evt.screenX);
}
 

 

 

Best Answer
0 Votes
I would do something like this trying to simulate a swipe event.
Compare the initial position of the x and the latest x position to know what way is the user swiping
let myButton = document.getElementById("myButton"); myButton.onmousedown = function(evt) { console.log("Before swipe x position: " + evt.screenX); } myButton.onmouseup = function(evt) { console.log("After swipe x position: " + evt.screenX); }

 

Best Answer
0 Votes

I would try something like this to simulate the swipe:

 

let myButton = document.getElementById("myButton");

myButton.onmousedown = function(evt) {
  console.log("Mouse moved: " + evt.screenX);
}

myButton.onmouseup = function(evt) {
  console.log("Mouse moved: " + evt.screenX);
}
Try to compare the x before the move and after the move to get the direction.

 

Best Answer
0 Votes

did you manage to get the swipes to work? if so could you share how you did this? 

Best Answer
0 Votes

Hi, I would try something like this to simulate the swipe movement:

 

var myButton = document.getElementById("myButton");

myButton.onmousedown = function(evt) {
  console.log("Mouse moved: " + evt.screenX);
}
myButton.onmouseup = function(evt) {
  console.log("Mouse moved: " + evt.screenX);
}

And compare the results to see what direction the user swiped.
Best Answer
0 Votes

What i use in My app is pretty simple but it works

 


var y = 0;
var x=0;
var timerText = document.getElementById("timer");
timerText.onmousedown = function(evt) {
  y = evt.screenY;
  x = evt.screenX;
}
timerText.onmouseup = function(evt) {
  let yMove = evt.screenY-y;
  let xMove = evt.screenX-x;
  if (yMove< -60) {//swipe up  };

  if (yMove> 60) {//swipe down};

  if (xMove< -60) {//swipe left  };

  if (xMove> 60) {//swipe right};


 
}

Best Answer

Thanks for the responses I'm going to give a shot tonight.

Best Answer
0 Votes

@Pietero the onmousedown / onmouseup worked great. 

Thanks

Best Answer
0 Votes