当前位置:首页 > 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进行节流。

let ticking = false;

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

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

js手势实现

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

相关文章

js实现轮播

js实现轮播

实现基础轮播效果 使用HTML结构创建轮播容器和图片元素: <div class="carousel"> <div class="carousel-inner">…

vue.js实现轮播

vue.js实现轮播

Vue.js 实现轮播功能 使用第三方库(推荐) Vue.js 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper,它们功能丰富且易于集成。 安装 swip…

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https:/…

js实现

js实现

实现 JavaScript 功能的方法 在 JavaScript 中实现特定功能通常涉及多个步骤。以下是常见的实现方法: 基础语法和变量声明 使用 let 或 const 声明变量: let co…

js实现动画

js实现动画

使用 CSS 动画与 JavaScript 控制 通过 JavaScript 动态添加或移除 CSS 类来触发动画。CSS 定义关键帧(@keyframes),JavaScript 通过 classL…