Does jQuery preserve touch events properties?

前端 未结 2 738
不思量自难忘°
不思量自难忘° 2020-12-08 01:40

While this code produces the expected behavior of \"1\" when touching the screen:

document.getElementById(\'someNodeId\').addEventListener(\'touchmove\', tou         


        
相关标签:
2条回答
  • 2020-12-08 01:55

    I use this simple function for JQuery based project

    var pointerEventToXY = function(e){
      var out = {x:0, y:0};
      if(e.type == 'touchstart' || e.type == 'touchmove' || e.type == 'touchend' || e.type == 'touchcancel'){
        var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
        out.x = touch.pageX;
        out.y = touch.pageY;
      } else if (e.type == 'mousedown' || e.type == 'mouseup' || e.type == 'mousemove' || e.type == 'mouseover'|| e.type=='mouseout' || e.type=='mouseenter' || e.type=='mouseleave') {
        out.x = e.pageX;
        out.y = e.pageY;
      }
      return out;
    };
    

    example:

    $('a').on('mousemove touchmove', function(e){
       console.log(pointerEventToXY(e)); // will return obj ..kind of {x:20,y:40}
    })
    

    hope this will be usefull for you ;)

    0 讨论(0)
  • 2020-12-08 02:05

    Try

    $(document).ready (function () {
        $("#someNodeId").bind("touchmove", function (event) {
            var e = event.originalEvent;
            console.log(e.targetTouches[0].pageX);
        });
    });
    
    0 讨论(0)
提交回复
热议问题