zoukankan      html  css  js  c++  java
  • 获取touchstart,touchmove,touchend 坐标

    简单说下如何用jQuery 和 js原生代码获取touchstart,touchmove,touchend 坐标值:

    jQuery 代码:

    $('#id').on('touchstart',function(e) {
        var _touch = e.originalEvent.targetTouches[0];
        var _x= _touch.pageX;
    });
     
    $('#id').on('touchmove',function(e) {
        var _touch = e.originalEvent.targetTouches[0];
        var _x= _touch.pageX;
    });
     
    $('#id').on('touchend',function(e) {
        var _touch = e.originalEvent.changedTouches[0];
        var _x= _touch.pageX;
    }

    js原生代码

    document.getElementById("id").addEventListener("touchstart",function(e)
    {
        var _x=e.touches[0].pageX;
        var _y=e.touches[0].pageY;
        console.log("start",_x)
    })
    document.getElementById("id").addEventListener("touchmove",function(e)
    {
        var _x=e.touches[0].pageX;
        var _y=e.touches[0].pageY;
        console.log("move",_x)
    })
    document.getElementById("id").addEventListener("touchend",function(e)
    {
        var _x=e.changedTouches[0].pageX;
        var _y=e.changedTouches[0].pageY;
        console.log("end",_x)
    })

    以上两种办法中 touchend 需要使用changedTouches[0]。

    一般我们取第一个手指的坐标,如果有其他要求,可能需要判断手指数量。

    if (e.targetTouches.length == 1)
    {
      //...
    }
  • 相关阅读:
    jquery mobile
    可能用到的负边距应用
    兼容性问题
    less和scss
    函数的继承
    关于canvas
    html5表单属性
    html代码
    git 拉取远程分支 --本地分支不存在
    git 删除分支
  • 原文地址:https://www.cnblogs.com/sese/p/7308750.html
Copyright © 2011-2022 走看看