开发者

iPhone/Android swipe detection for mobile browser

开发者 https://www.devze.com 2023-01-05 18:53 出处:网络
Is it possible to detect that the viewport is being dragged by a touch event? Using the following code I am able to get the position of where the finger touched the screen, what node started the even

Is it possible to detect that the viewport is being dragged by a touch event?

Using the following code I am able to get the position of where the finger touched the screen, what node started the event and where it was dragged to. Which solves one of the problems but I would really like to detect when the user has dragged the page/window/viewport down.

To attempt to be more clear as to what I am trying to do: I would like simulate the refresh activity in Tweetie 2/Twitter for iPhone but in HTML5 and JavaScript.

<script> 
window.ontouchmove = function(e){
  if(e.touches.length == 1){ // Only deal with one finger
    var touch = e.touches[0]; // Get the information for finger #1
    var node = touch.target; // Find the node the drag started from
    $("p:last").html(
        "clienty: " + touch.clientY + "<br/>" +
        "screenY: "开发者_JS百科 + touch.screenY + "<br/>" +
        "pageY: "   + touch.pageY
        );
  }
}
</script> 


In the touchmove event, the event.targetTouches[0].pageX and .pageY properties will give you a running indication of the touch coordinates. You'll need to store the original touch location in touchstart, update the current location in touchmove, and then in touchend, you determine whether the touch has been up, down, left, right by calculating the differences on the x and y axes between the starting and ending locations. The length of the touches array will let you know whether it's a one-finger swipe or a two-finger pinch.


You might want to check out Sencha Touch. It's a mobile framework that supports those events, so you don't have to roll your own support.

0

精彩评论

暂无评论...
验证码 换一张
取 消