当前位置:首页 > JavaScript

js手势实现

2026-01-14 14:14:23JavaScript

手势识别实现方法

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

基础事件监听

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进行节流。

js手势实现

let ticking = false;

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

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

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

相关文章

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js钟表实现

js钟表实现

实现JavaScript钟表的基本方法 创建一个简单的JavaScript钟表可以通过以下步骤完成,涵盖数字和模拟两种形式。 数字钟表实现 HTML结构只需一个显示时间的容器: <div i…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…

js实现跑马灯

js实现跑马灯

实现跑马灯效果 使用HTML和JavaScript可以轻松实现跑马灯效果。以下是两种常见的实现方式: HTML结构 <div id="marquee"> <span>…

js实现按钮点击

js实现按钮点击

实现按钮点击的JavaScript方法 HTML按钮元素 在HTML中创建按钮元素,为其添加id或class以便JavaScript选择: <button id="myButton">点…