JS实现移动端双转盘:原生Touch事件监听与联动逻辑

作者:页面旅途

时间:2026-12-05

浏览:125

使用原生 touchstart 、 touchmove 和 touchend 事件监听手指滑动,通过计算触摸点与转盘中心的夹角变化量来驱动旋转。双转盘联动逻辑的核心在于:当内盘旋转时,根据预设的传动比(如 1:2)同步更新外盘的 CSS transform 属性;反之亦然。关键在于处理 touchmove 中的角度累加

文章目录

使用原生 touchstart、touchmove 和 touchend 事件监听手指滑动,通过计算触摸点与转盘中心的夹角变化量来驱动旋转。双转盘联动逻辑的核心在于:当内盘旋转时,根据预设的传动比(如 1:2)同步更新外盘的 CSS transform 属性;反之亦然。关键在于处理 touchmove 中的角度累加和边界回弹。

JS实现移动端双转盘:原生Touch事件监听与联动逻辑

触摸角度计算与单盘旋转原理

移动端浏览器对鼠标事件支持不佳,必须使用 Touch API。核心思路是获取触摸起始点的坐标,计算其与圆盘中心的初始夹角,随后在移动过程中实时计算新夹角,两者的差值即为旋转增量。

JS实现移动端双转盘:原生Touch事件监听与联动逻辑

我们需要定义一个辅助函数来计算两点间的夹角。利用 Math.atan2(y, x) 可以获取弧度,再转换为角度。为了防止旋转方向混乱,需要记录上一帧的角度或累计角度。对于单个转盘,只需将计算出的 deltaAngle 累加到当前的 totalRotation 变量中,并应用 transform: rotate()。这里要注意,touchmove 触发频率很高,直接操作 DOM 可能导致卡顿,建议结合 requestAnimationFrame 或直接更新样式属性,现代浏览器对简单样式变更优化良好。

双转盘联动逻辑与代码实现

将上述单盘逻辑扩展为双盘。假设内盘(Inner)和外盘(Outer)存在机械联动关系,例如内盘转一圈,外盘转半圈(传动比 0.5)。我们在同一个 touchmove 处理器中同时计算两个盘的旋转角度。

JS实现移动端双转盘:原生Touch事件监听与联动逻辑

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Dual Dial Demo</title>
<style>
  body { margin: 0; overflow: hidden; background: #f0f0f0; display: flex; justify-content: center; align-items: center; height: 100vh; }
  .dial-container { position: relative; width: 300px; height: 300px; }
  .dial { position: absolute; border-radius: 50%; border: 2px solid #ccc; box-sizing: border-box; display: flex; justify-content: center; align-items: center; font-size: 20px; font-weight: bold; color: #333; }
  #outer-dial { width: 300px; height: 300px; background: #e0e0e0; top: 0; left: 0; z-index: 1; }
  #inner-dial { width: 150px; height: 150px; background: #4a90e2; color: white; top: 75px; left: 75px; z-index: 2; touch-action: none; }
  /* 指示标记 */
  .marker { position: absolute; width: 4px; height: 20px; background: red; top: -20px; left: 50%; transform: translateX(-50%); }
</style>
</head>
<body>
<div class="dial-container">
  <div id="outer-dial" class="dial"><div class="marker"></div></div>
  <div id="inner-dial" class="dial"><div class="marker"></div></div>
</div>

<script>
const innerDial = document.getElementById('inner-dial');
const outerDial = document.getElementById('outer-dial');

// 传动比:内盘转1度,外盘转0.5度
const RATIO = 0.5;

let currentInnerRotation = 0;
let currentOuterRotation = 0;

let isDragging = false;
let startY = 0;
let startRotation = 0;

// 初始化位置
innerDial.style.transform = `rotate(${currentInnerRotation}deg)`;
outerDial.style.transform = `rotate(${currentOuterRotation}deg)`;

innerDial.addEventListener('touchstart', (e) => {
  isDragging = true;
  startY = e.touches[0].clientY;
  startRotation = currentInnerRotation;
  // 阻止默认滚动
  e.preventDefault();
}, { passive: false });

innerDial.addEventListener('touchmove', (e) => {
  if (!isDragging) return;
  e.preventDefault();
  
  const currentY = e.touches[0].clientY;
  const deltaY = startY - currentY; // 向上滑为正
  
  // 简单的线性映射:每移动1px对应1度旋转(可根据灵敏度调整)
  const deltaRotation = deltaY * 1.5;
  
  currentInnerRotation = startRotation + deltaRotation;
  currentOuterRotation = currentInnerRotation * RATIO;
  
  innerDial.style.transform = `rotate(${currentInnerRotation}deg)`;
  outerDial.style.transform = `rotate(${currentOuterRotation}deg)`;
}, { passive: false });

innerDial.addEventListener('touchend', () => {
  isDragging = false;
});
</script>
</body>
</html>

运行该 HTML 文件,用手指拖动蓝色的内盘。可以看到绿色的外盘以一半的速度同步旋转。代码中通过 touchmove 计算 Y 轴位移差并映射为旋转角度,实现了基本的联动。若需实现惯性滑动,需在 touchend 中记录速度并启动动画循环。注意 touch-action: none 确保了转盘区域不会触发页面滚动。

在实际开发中,通常只有一个触摸源控制一个主转盘,另一个从动转盘跟随。如果用户意图是独立控制两个盘,则需要更复杂的多点触控逻辑,但大多数 UI 场景下是“拨动内盘带动外盘”或“拖拽背景”。以下代码演示了最通用的“内盘主动,外盘被动”场景。代码保存为 dial.html,直接在手机浏览器打开即可测试。拖动蓝色内盘,绿色外盘会按比例旋转。

防止滚动冲突与惯性滑动处理

在移动端网页中,touchmove 默认会触发页面滚动,导致转盘无法顺畅旋转。必须在触摸开始时调用 event.preventDefault() 阻止默认行为。另外,为了提升手感,通常需要添加惯性滑动(Fling)效果,即手指松开后转盘继续旋转并逐渐减速停止。

惯性滑动的实现依赖于记录触摸结束瞬间的速度。在 touchend 事件中,我们记录最终几次 touchmove 的位置和时间戳,计算出角速度。然后使用 requestAnimationFrame 启动一个衰减循环,每帧将当前速度乘以一个衰减系数(如 0.95),直到速度接近零。这能让转盘有自然的物理质感,而不是生硬地停在原地。还需注意 touch-action: none CSS 属性的设置,它能从根本上禁止浏览器对该元素的默认触摸行为,简化 JS 逻辑。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章