javascript - 每 x 毫秒获取 jquery 触摸位置

标签 javascript jquery html touch touch-event

有没有一种方法可以在每 x 毫秒的 touchmove 事件中获取触摸位置,然后执行一个函数,当此时的 x 坐标与开始时的 x 坐标不同时,例如50 像素?

谢谢

最佳答案

尝试以下操作;

$('document').ready(function() {

  var touch,
    action,
    diffX,
    diffY,
    endX,
    endY,
    startX,
    startY,
    timer,
    timerXseconds = 500, // Change to the Time(milliseconds) to check for touch position 
    xDifferenceX = 50, // Change to difference (px) for x-coordinates from starting point to run your function
    xDifferenceY = 50; // Change to difference (px) for y-coordinates from starting point 

  function getCoord(e, c) {
    return /touch/.test(e.type) ? (e.originalEvent || e).changedTouches[0]['page' + c] : e['page' + c];
  }

  function testTouch(e) {
    if (e.type == 'touchstart') {
      touch = true;
    } else if (touch) {
      touch = false;
      return false;
    }
    return true;
  }

  function onStart(ev) {
    if (testTouch(ev) && !action) {
      action = true;
      startX = getCoord(ev, 'X');
      startY = getCoord(ev, 'Y');
      diffX = 0;
      diffY = 0;
      timer = window.setInterval(checkPosition(ev), timerXseconds); // get coordinaties ever X time
      if (ev.type == 'mousedown') {
        $(document).on('mousemove', onMove).on('mouseup', onEnd);
      }

    }
  }

  function onMove(ev) {
    if (action) {
      checkPosition(ev)
    }
  }

  function checkPosition(ev) {
    endX = getCoord(ev, 'X');
    endY = getCoord(ev, 'Y');
    diffX = endX - startX;
    diffY = endY - startY;
    // Check if coordinates on Move are Different than Starting point by X pixels 
    if (Math.abs(diffX) > xDifferenceX || Math.abs(diffY) > xDifferenceY) {
    //  console.log('Start is :' + startX + ' End is : ' + endX + 'Difference is : ' + diffX);
      $(this).trigger('touchend');

      // here Add your function to run...
    }

  }

  function onEnd(ev) {
    window.clearInterval(timer);
    if (action) {
      action = false;
      if (ev.type == 'mouseup') {
        $(document).off('mousemove', onMove).off('mouseup', onEnd);
      }
    }
  }

  $('#monitor')
    .bind('touchstart mousedown', onStart)
    .bind('touchmove', onMove)
    .bind('touchend touchcancel', onEnd);
});
body {
  margin: 0;
  padding: 0;
}
#monitor {
  height: 500px;
  width: 500px;
  position: relative;
  display: block;
  left: 50px;
  top: 50px;
  background: green;
}
.box {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  text-align: center;
  font-weight: bold;
  bottom: 0;
  background: white;
  width: 50px;
  height: 50px;
  margin: auto;
  font-size: 16px;
  line-height: 23px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id='monitor'>
  <div class='box'>start here</div>
</div>

阅读此内容 post更详细的答案

关于javascript - 每 x 毫秒获取 jquery 触摸位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34993530/

相关文章:

html - 屏幕较短时页面顶部消失

javascript - 这些代码行在某种意义上是相等的吗?

javascript - 支持 &lt;textarea&gt; 中的缩进选项卡

javascript - Flatpickr + SweetAlert2 = 键盘辅助功能问题

javascript - 通过移动浏览器访问 Google AAID (Javascript)

javascript - 延迟 jquery ajax 请求的顺序

Javascript - 将两个输入字段相乘并显示

javascript - e.target.value 的值未定义

javascript - 在同一页面上多次调用 jQuery.noConflict()

javascript - 在javascript中禁用visual studio自动完成评论