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

JavaScript模拟滑动手势

双击回到顶部

左滑动

 

右滑动

 

代码展示 

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Gesture Example</title><style>body {margin: 0;height: 200vh;} /* 模拟长页面 */.notification {position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);padding: 20px;background-color: rgba(0, 0, 0, 0.7);color: white;font-size: 24px;border-radius: 10px;opacity: 0;transition: opacity 0.5s ease, transform 0.5s ease;}.notification.show {opacity: 1;transform: translate(-50%, -50%) scale(1.1);}</style></head><body><div class="notification" id="notification"></div><div style="height: 30vh; background-color: rgb(178, 129, 129)"></div><div style="height: 30vh; background-color: rgb(141, 40, 40)"></div><div style="height: 30vh; background-color: rgb(135, 85, 85)"></div><div style="height: 30vh; background-color: rgb(209, 188, 188)"></div><div style="height: 30vh; background-color: rgb(203, 44, 44)"></div><script>//在dom完全加载后执行document.addEventListener("DOMContentLoaded", () => {let touchStartX = 0;let touchStartY = 0;let touchEndX = 0;let touchEndY = 0;let lastTap = 0;const notification = document.getElementById("notification");//监听开始事件document.addEventListener("touchstart", (e) => {touchStartX = e.changedTouches[0].screenX;touchStartY = e.changedTouches[0].screenY;});//监听移动事件document.addEventListener("touchmove", (e) => {touchEndX = e.changedTouches[0].screenX;touchEndY = e.changedTouches[0].screenY;});//监听结束事件document.addEventListener("touchend", () => {handleGesture();});//监听双击事件document.addEventListener("touchend", (e) => {const currentTime = new Date().getTime();const tapLength = currentTime - lastTap;if (tapLength < 300 && tapLength > 0) {// 双击事件window.scrollTo({ top: 0, behavior: "smooth" });}lastTap = currentTime;});//判断手势function handleGesture() {const deltaX = touchEndX - touchStartX;const deltaY = touchEndY - touchStartY;if (Math.abs(deltaX) > Math.abs(deltaY)) {if (deltaX > 50) {showNotification("右滑动");} else if (deltaX < -50) {showNotification("左滑动");}}}//显示通知function showNotification(message) {notification.textContent = message;notification.classList.add("show");setTimeout(() => {notification.classList.remove("show");}, 1000);}});</script></body>
</html>

 

http://www.lryc.cn/news/407235.html

相关文章:

  • Text Control 控件教程:使用 .NET C# 中的二维码和条形码增强文档
  • 最新爆火的开源AI项目 | LivePortrait 本地安装教程
  • 揭秘Django与Neo4j:构建智能知识图谱的终极指南
  • 项目一缓存商品
  • SEO与数据中心代理IP的结合能带来哪些便利?
  • 《昇思25天学习打卡营第6天|ResNet50图像分类》
  • Activiti 6 兼容openGauss数据库bytes类型不匹配
  • 缓存技术:提升性能与效率的利器
  • LeetCode 637, 67, 399
  • 如何压缩视频大小不改变画质?这5个视频压缩免费软件超好用!
  • 深入理解 Java 虚拟机第三版(周志明)
  • 算法 定长按组翻转链表
  • 安装nfs和rpcbind设置linux服务器共享磁盘
  • 物联网在电力行业的应用
  • Java 代码规范if嵌套
  • ASPICE如何确保汽车软件产品质量的稳固基石
  • 【深度学习】yolov8-seg分割训练,拼接图的分割复原
  • Python升级打怪—Django入门
  • leetcode面试题17.最大子矩阵
  • 计算机网络:构建联结的基础
  • node和npm安装;electron、 electron-builder安装
  • 操作系统概念(黑皮书)阅读笔记
  • matlab gui下的tcp client客户端编程框架
  • Matplotlib : Python 的绘图库
  • 数据编织 VS 数据仓库 VS 数据湖
  • CSS(十一)——CSS分组和嵌套,尺寸(Dimension)
  • 必备神器!三款优秀远程控制电脑软件推荐
  • 关于正运动学解机器人手臂算法
  • MySQL 约束 (constraint)
  • 用python程序发送文件(python实例二十六)