找出增量距离?

时间:2020-01-22 20:57:33

标签: javascript delta

如何找到距触摸中心的距离?

例如,我们触摸,如果我们向上拉,则得到1 23。如果我们向下拉,则得到-1 -2 -3。

我知道可以在保存的位置上进行操作(开始触摸)。但是我自己想不到。谢谢!

// touchstart
window.addEventListener('touchmove', touchmove);

var deltaY;

function touchmove(e) {
  deltaY = e.touches[0].clientY;
  console.log(deltaY);
}

1 个答案:

答案 0 :(得分:2)

您可以创建2个事件监听器,一个用于捕获开始位置,另一个用于捕获移动事件:

document.body.addEventListener('touchmove', touchmove);
document.body.addEventListener('touchstart', touchstart);


var startX, startY;

function touchstart(e)
{
    startX = e.touches[0].clientX;
    startY = e.touches[0].clientY;
}

function touchmove(e)
{
  var deltaX = e.touches[0].clientX - startX,
        deltaY = e.touches[0].clientY - startY;


console.log('Delta x,y',deltaX, deltaY);
}
相关问题