当前位置:首页 > JavaScript

js手势实现

2026-01-14 14:14:23JavaScript

手势识别实现方法

在JavaScript中实现手势识别通常涉及监听触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseup),并通过计算移动距离、速度等参数来判断手势类型。

js手势实现

基础事件监听

const element = document.getElementById('gestureArea');
let startX, startY;

element.addEventListener('touchstart', (e) => {
  startX = e.touches[0].clientX;
  startY = e.touches[0].clientY;
});

element.addEventListener('touchmove', (e) => {
  const moveX = e.touches[0].clientX - startX;
  const moveY = e.touches[0].clientY - startY;

  // 手势判断逻辑
});

常见手势实现

滑动检测

element.addEventListener('touchmove', (e) => {
  const threshold = 50; // 滑动阈值
  const moveX = e.touches[0].clientX - startX;

  if (Math.abs(moveX) > threshold) {
    if (moveX > 0) {
      console.log('向右滑动');
    } else {
      console.log('向左滑动');
    }
  }
});

缩放检测

let initialDistance;

element.addEventListener('touchstart', (e) => {
  if (e.touches.length >= 2) {
    initialDistance = Math.hypot(
      e.touches[0].clientX - e.touches[1].clientX,
      e.touches[0].clientY - e.touches[1].clientY
    );
  }
});

element.addEventListener('touchmove', (e) => {
  if (e.touches.length >= 2) {
    const currentDistance = Math.hypot(
      e.touches[0].clientX - e.touches[1].clientX,
      e.touches[0].clientY - e.touches[1].clientY
    );

    const scale = currentDistance / initialDistance;
    console.log('缩放比例:', scale);
  }
});

旋转检测

element.addEventListener('touchstart', (e) => {
  if (e.touches.length >= 2) {
    initialAngle = Math.atan2(
      e.touches[1].clientY - e.touches[0].clientY,
      e.touches[1].clientX - e.touches[0].clientX
    );
  }
});

element.addEventListener('touchmove', (e) => {
  if (e.touches.length >= 2) {
    const currentAngle = Math.atan2(
      e.touches[1].clientY - e.touches[0].clientY,
      e.touches[1].clientX - e.touches[0].clientX
    );

    const rotation = (currentAngle - initialAngle) * (180 / Math.PI);
    console.log('旋转角度:', rotation);
  }
});

使用手势库

对于更复杂的手势识别,可以使用现成的库如Hammer.js:

const hammertime = new Hammer(element);

hammertime.on('pan', (e) => {
  console.log('平移:', e.deltaX, e.deltaY);
});

hammertime.on('pinch', (e) => {
  console.log('缩放:', e.scale);
});

hammertime.on('rotate', (e) => {
  console.log('旋转:', e.rotation);
});

性能优化建议

避免在touchmove事件中执行过多计算或DOM操作,使用requestAnimationFrame进行节流。

let ticking = false;

element.addEventListener('touchmove', (e) => {
  if (!ticking) {
    requestAnimationFrame(() => {
      // 执行手势处理逻辑
      ticking = false;
    });
    ticking = true;
  }
});

以上方法提供了从基础到高级的手势识别实现方案,可以根据具体需求选择合适的实现方式。

js手势实现

标签: 手势js
分享给朋友:

相关文章

js 实现继承

js 实现继承

原型链继承 通过让子类的原型对象指向父类的实例来实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Pa…

js实现乘

js实现乘

JavaScript 实现乘法运算 在 JavaScript 中实现乘法运算可以通过多种方式完成,包括基本运算符、函数封装以及高级算法(如大数乘法)。以下是几种常见方法: 使用基本乘法运算符 Jav…

利用js实现

利用js实现

使用 JavaScript 实现 在 JavaScript 中,可以通过多种方式实现功能,具体取决于需求。以下是几种常见的方法: 方法一:使用原生 JavaScript // 示例代码…

vue实现js休眠

vue实现js休眠

实现 JavaScript 休眠的方法 在 Vue 中实现 JavaScript 休眠(延迟执行)可以通过以下方式实现。由于 JavaScript 本身没有内置的 sleep 函数,通常使用 Prom…

js实现正交

js实现正交

正交的概念 正交在数学和计算机科学中通常指两个向量垂直或线性无关。在编程中,正交性常被用于设计模块化、低耦合的系统。 向量正交判断 判断两个向量是否正交可以通过点积是否为0来实现: fun…

js 实现跳转

js 实现跳转

使用 window.location.href 进行跳转 通过修改 window.location.href 可以跳转到指定 URL,浏览器会加载新页面: window.location.hr…