jQuery (Swipe vs. Touch) pageX and pageY keep returning 0

前端 未结 4 922
忘掉有多难
忘掉有多难 2020-12-25 14:10

I\'m playing with touchstart and touchend events on my iPhone. I built a sample page that has div that if you touch it and scroll the page, it should return the y coordinate

4条回答
  •  孤城傲影
    2020-12-25 14:52

    Here is what works for me....

    $('.someClass').bind('touchmove',function(e){
      e.preventDefault();
      var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
      var elm = $(this).offset();
      var x = touch.pageX - elm.left;
      var y = touch.pageY - elm.top;
      if(x < $(this).width() && x > 0){
          if(y < $(this).height() && y > 0){
                  //your code here
          }
         }
    });
    

提交回复
热议问题