How to implement swipe gestures for mobile devices?

后端 未结 9 1065
野性不改
野性不改 2020-12-12 14:58

I have an application made in AngularJS which has arrow key navigation to switch views.

I want to implement this navigation using swipe for touch devices. I tried jG

相关标签:
9条回答
  • 2020-12-12 15:49

    Hammer time!

    I have used Hammer JS and it work with gesture. Read details from here: https://hammerjs.github.io/

    Good thing that it is much more light weight and fast then jQuery mobile. You can test it on their website as well.

    0 讨论(0)
  • 2020-12-12 15:52

    I made this function for my needs.

    Feel free to use it. Works great on mobile devices.

    function detectswipe(el,func) {
      swipe_det = new Object();
      swipe_det.sX = 0; swipe_det.sY = 0; swipe_det.eX = 0; swipe_det.eY = 0;
      var min_x = 30;  //min x swipe for horizontal swipe
      var max_x = 30;  //max x difference for vertical swipe
      var min_y = 50;  //min y swipe for vertical swipe
      var max_y = 60;  //max y difference for horizontal swipe
      var direc = "";
      ele = document.getElementById(el);
      ele.addEventListener('touchstart',function(e){
        var t = e.touches[0];
        swipe_det.sX = t.screenX; 
        swipe_det.sY = t.screenY;
      },false);
      ele.addEventListener('touchmove',function(e){
        e.preventDefault();
        var t = e.touches[0];
        swipe_det.eX = t.screenX; 
        swipe_det.eY = t.screenY;    
      },false);
      ele.addEventListener('touchend',function(e){
        //horizontal detection
        if ((((swipe_det.eX - min_x > swipe_det.sX) || (swipe_det.eX + min_x < swipe_det.sX)) && ((swipe_det.eY < swipe_det.sY + max_y) && (swipe_det.sY > swipe_det.eY - max_y) && (swipe_det.eX > 0)))) {
          if(swipe_det.eX > swipe_det.sX) direc = "r";
          else direc = "l";
        }
        //vertical detection
        else if ((((swipe_det.eY - min_y > swipe_det.sY) || (swipe_det.eY + min_y < swipe_det.sY)) && ((swipe_det.eX < swipe_det.sX + max_x) && (swipe_det.sX > swipe_det.eX - max_x) && (swipe_det.eY > 0)))) {
          if(swipe_det.eY > swipe_det.sY) direc = "d";
          else direc = "u";
        }
    
        if (direc != "") {
          if(typeof func == 'function') func(el,direc);
        }
        direc = "";
        swipe_det.sX = 0; swipe_det.sY = 0; swipe_det.eX = 0; swipe_det.eY = 0;
      },false);  
    }
    
    function myfunction(el,d) {
      alert("you swiped on element with id '"+el+"' to "+d+" direction");
    }
    

    To use the function just use it like

    detectswipe('an_element_id',myfunction);
    
    detectswipe('an_other_element_id',my_other_function);
    

    If a swipe is detected the function "myfunction" is called with parameter element-id and "l,r,u,d" (left,right,up,down).

    Example: http://jsfiddle.net/rvuayqeo/1/


    I (UlysseBN) made a new version of this script based on this one which use more modern JavaScript, it looks like it behaves better on some cases. If you think it should rather be an edit of this answer let me know, if you are the original author and you end up editing, I'll delete my answer.

    0 讨论(0)
  • 2020-12-12 15:53

    Shameless plug I know, but you might want to consider a jQuery plugin that I wrote:

    https://github.com/benmajor/jQuery-Mobile-Events

    It does not require jQuery Mobile, only jQuery.

    0 讨论(0)
提交回复
热议问题