当前位置: 首页 > ai >正文

判断手机屏幕上的横向滑动(左滑和右滑)

在JavaScript中,你可以通过监听触摸事件(touch events)来判断用户在手机屏幕上的横向滑动方向。以下是实现方法:

基本实现方案

let touchStartX = 0;
let touchEndX = 0;function handleTouchStart(event) {touchStartX = event.changedTouches[0].screenX;
}function handleTouchEnd(event) {touchEndX = event.changedTouches[0].screenX;handleSwipe();
}function handleSwipe() {const threshold = 50; // 最小滑动距离阈值const diffX = touchEndX - touchStartX;if (Math.abs(diffX) > threshold) {if (diffX > 0) {console.log("向右滑动");// 执行右滑操作} else {console.log("向左滑动");// 执行左滑操作}}
}// 添加事件监听
const element = document.getElementById('swipe-area');
element.addEventListener('touchstart', handleTouchStart, false);
element.addEventListener('touchend', handleTouchEnd, false);

更完善的实现(包含滑动速度检测)

let touchStartX = 0;
let touchStartTime = 0;const element = document.getElementById('swipe-area');element.addEventListener('touchstart', (e) => {touchStartX = e.touches[0].clientX;touchStartTime = Date.now();
}, false);element.addEventListener('touchmove', (e) => {e.preventDefault(); // 阻止默认行为,避免页面滚动
}, { passive: false });element.addEventListener('touchend', (e) => {const touchEndX = e.changedTouches[0].clientX;const touchEndTime = Date.now();const diffX = touchEndX - touchStartX;const duration = touchEndTime - touchStartTime;const velocity = Math.abs(diffX) / duration; // 计算滑动速度const threshold = 50; // 最小滑动距离const minVelocity = 0.3; // 最小滑动速度(像素/毫秒)if (Math.abs(diffX) > threshold && velocity > minVelocity) {if (diffX > 0) {console.log("向右滑动");// 执行右滑操作} else {console.log("向左滑动");// 执行左滑操作}}
}, false);

React 组件实现示例

import React, { useRef } from 'react';const SwipeDetector = () => {const touchStartX = useRef(0);const touchStartTime = useRef(0);const handleTouchStart = (e) => {touchStartX.current = e.touches[0].clientX;touchStartTime.current = Date.now();};const handleTouchEnd = (e) => {const touchEndX = e.changedTouches[0].clientX;const touchEndTime = Date.now();const diffX = touchEndX - touchStartX.current;const duration = touchEndTime - touchStartTime.current;const velocity = Math.abs(diffX) / duration;const threshold = 50;const minVelocity = 0.3;if (Math.abs(diffX) > threshold && velocity > minVelocity) {if (diffX > 0) {console.log("向右滑动");// 执行右滑操作} else {console.log("向左滑动");// 执行左滑操作}}};return (<divstyle={{ width: '100%', height: '200px', backgroundColor: '#eee' }}onTouchStart={handleTouchStart}onTouchEnd={handleTouchEnd}>在此区域滑动</div>);
};export default SwipeDetector;

注意事项

  1. 阈值选择threshold 值需要根据实际需求调整,太小可能会误触,太大可能导致滑动不灵敏
  2. 性能考虑:避免在touchmove事件中执行复杂操作
  3. 兼容性:现代浏览器都支持触摸事件,但要注意某些旧版本浏览器的兼容性
  4. 被动事件:如果不需要阻止默认行为,可以设置{ passive: true }提高滚动性能
  5. 多指触摸:上述代码只处理单指滑动,如需处理多指手势需要额外逻辑

进阶优化

  1. 可以添加垂直滑动检测,并判断是横向还是纵向滑动
  2. 可以添加滑动过程中的视觉反馈
  3. 可以使用现有的手势库如Hammer.js(https://hammerjs.github.io/)简化开发
<!-- 使用Hammer.js示例 -->
<script src="https://cdn.jsdelivr.net/npm/hammerjs@2.0.8/hammer.min.js"></script>
<script>const element = document.getElementById('swipe-area');const hammer = new Hammer(element);hammer.on('swipeleft', () => console.log('向左滑动'));hammer.on('swiperight', () => console.log('向右滑动'));
</script>
http://www.xdnf.cn/news/9272.html

相关文章:

  • 智慧康养护理:科技重塑老龄化社会的健康守护体系
  • 攻防世界逆向刷题笔记(新手模式9-1?)
  • (1)课堂 1--5,这五节主要讲解 mysql 的概念,定义,下载安装与卸载
  • LiveData的优势
  • Oracle EBS 12.1 处理ISG 发布的wsdl 被请求时遇到500错误
  • Docker常用操作
  • 无人机多人协同控制技术解析
  • linux ls命令显示目录颜色调整
  • ntp 时钟同步
  • 无人机停机坪运行技术分析!
  • OpenGL Chan视频学习-10 Dealing with Errors in OpenGL
  • c++数据结构4——链表结构详解
  • 【DSP笔记】掌握数字世界的律动:时域离散信号与系统基础
  • React - 封装礼物PK条组件
  • winform LiveCharts2的使用--图表的使用
  • 小土堆pytorch--现有网络模型的使用及修改
  • 数据结构中无向图的邻接矩阵详解
  • 鸿蒙OSUniApp 实现的数据可视化图表组件#三方框架 #Uniapp
  • Rust 学习笔记:迭代器
  • 组合型回溯+剪枝
  • OpenCV CUDA模块图像处理------颜色空间处理之颜色空间转换函数cvtColor()
  • Axure中继器学习笔记
  • DB2数据库HADR配置及详解
  • Femap许可证与网络安全策略
  • arcgis字段计算器中计算矢量面的每个点坐标
  • vscode开发stm32,main.c文件中出现很多报错影响开发解决日志
  • 【脚本】一键部署脚本
  • 深入理解设计模式之命令模式
  • 公共场所人脸识别设备备案合规要点
  • [STM32学习笔记(九)]CubeMX项目使用系统定时器SysTick的中断服务函数进行定时