4

I want to detect if users swipe from the edge or not before I open the panel with $("#panel").panel("open");. Here is the code

$("#main").on("swiperight",function(event){
var data = event.originalEvent.touches ? event.originalEvent.touches[0] : event,
    coords = [data.pageX, data.pageY];

console.log(coords);
});

However the coords didn't return anything because I got error:

Uncaught TypeError: Cannot read property 'touches' of undefined

  1. So is there a way to get coordinate when swipe happens?

  2. Or is there an easy way to detect if the staring position is from the left edge instead?

Thanks.

HP.
  • 19,226
  • 53
  • 154
  • 253
  • Similar: http://stackoverflow.com/questions/24163202/javascript-touch-movement-track-when-user-swipes-from-edges – trante Jun 12 '14 at 08:14

3 Answers3

4

Try below in jqm 1.4+, worked for me, adjust the edge trim value accordingly, 50 was good for me

$( "div.ui-page" ).on( "swiperight", function( e ) {
    if ( e.swipestart.coords[0] <50) {
    // your logic 
    }
});

this is for x coordinate similarly you can get y from coords[1]

Deepu
  • 1,241
  • 1
  • 11
  • 24
3

this will work for any screen size:

$("#main").on("swiperight",function( event ){
   // take 15% of screen good for diffrent screen size
   var window_width_15p = $( window ).width() * 0.15;
   // check if the swipe right is from 15% of screen (coords[0] means X)
   if ( event.swipestart.coords[0] < window_width_15p) {
      // open your panel
      $("#panel").panel("open");
   }
});
0

You can do something like this.

$('#main').on('touchstart', function(e) {
   var xPos = e.originalEvent.touches[0].pageX;
   if(xPos == 0) { //Keep in mind the margin of failure from 0, when user touches.
      //Handle edge swipe
   }
});
Rayf
  • 451
  • 3
  • 12
  • `touchstart` doesn't tell me if the swipe happened or not and how long the swipe is. Because I also set `$.event.special.swipe.horizontalDistanceThreshold = 120;` to make sure swipe is long enough. – HP. Nov 21 '13 at 00:10
  • To clarify I answered the two questions you asked. To get the full solution you need to combine your own solution to detect the swiperight event and my example to see when the touchstarts. If the touch start is not at the correct edge position you simply just don't handle the edge swipe. – Rayf Nov 21 '13 at 00:14