JS移动端双转盘怎么实现惯性滚动与自动回弹效果

作者:微光时间:2026-12-05浏览:299

使用 CSS transition 配合 JS 触摸事件监听,在滑动结束时计算目标角度并应用样式实现惯性滚动与自动回弹。

JS移动端双转盘怎么实现惯性滚动与自动回弹效果

转盘结构与触摸事件绑定

双转盘布局通常采用绝对定位叠加,通过 touchstart、touchmove 和 touchend 记录位移差,计算旋转角度。

JS移动端双转盘怎么实现惯性滚动与自动回弹效果

核心逻辑在于实时计算手指滑动的距离与方向。当用户开始触摸时,记录初始位置;移动过程中,根据位移更新转盘的即时角度,此时不添加过渡效果以保证跟手性;松开手指后,根据滑动速度和距离判断是否触发惯性,若未满足惯性条件则执行回弹动画回到最近的有效刻度或中心位置。

惯性算法与回弹实现

将完整程序保存为 dual-wheel.html,直接在浏览器移动端模式或真机中打开即可运行测试。

JS移动端双转盘怎么实现惯性滚动与自动回弹效果

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dual Wheel Inertia</title>
<style>
  body { display: flex; justify-content: center; align-items: center; height: 100vh; background: #f0f0f0; margin: 0; }
  .wheel-container { position: relative; width: 300px; height: 300px; }
  .wheel { 
    position: absolute; top: 0; left: 0; width: 100%; height: 100%; 
    border-radius: 50%; transition: transform 0.3s ease-out; 
    background: white; box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    display: flex; justify-content: center; align-items: center;
    font-size: 24px; color: #333;
  }
  .wheel-inner { pointer-events: none; }
  .indicator { 
    position: absolute; top: -10px; left: 50%; transform: translateX(-50%); 
    width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; 
    border-top: 20px solid red; z-index: 10;
  }
</style>
</head>
<body>
<div class="wheel-container">
  <div class="indicator"></div>
  <div class="wheel" id="wheel1">Wheel 1</div>
  <div class="wheel" id="wheel2">Wheel 2</div>
</div>

<script>
class WheelHandler {
  constructor(element) {
    this.el = element;
    this.startAngle = 0;
    this.currentAngle = 0;
    this.isDragging = false;
    this.lastY = 0;
    
    this.el.addEventListener('touchstart', (e) => this.handleStart(e));
    this.el.addEventListener('touchmove', (e) => this.handleMove(e));
    this.el.addEventListener('touchend', () => this.handleEnd());
  }

  handleStart(e) {
    this.isDragging = true;
    this.lastY = e.touches[0].clientY;
    this.el.style.transition = 'none'; // Disable transition for direct follow
  }

  handleMove(e) {
    if (!this.isDragging) return;
    const deltaY = this.lastY - e.touches[0].clientY;
    this.currentAngle += deltaY * 2; // Sensitivity factor
    this.el.style.transform = `rotate(${this.currentAngle}deg)`;
    this.lastY = e.touches[0].clientY;
  }

  handleEnd() {
    this.isDragging = false;
    this.el.style.transition = 'transform 0.5s cubic-bezier(0.25, 0.1, 0.25, 1)';
    
    // Simple snap to nearest 90 degrees for demo
    const snapAngle = Math.round(this.currentAngle / 90) * 90;
    this.currentAngle = snapAngle;
    this.el.style.transform = `rotate(${this.currentAngle}deg)`;
  }
}

const w1 = new WheelHandler(document.getElementById('wheel1'));
const w2 = new WheelHandler(document.getElementById('wheel2'));
// Note: wheel2 needs specific positioning in CSS to overlap correctly in real app
</script>
</body>
</html>

程序输出为两个可旋转的圆盘。输入为手指上下滑动,代码通过累加 deltaY 更新 currentAngle。在 touchend 时,通过四舍五入到 90 度的倍数实现自动回弹吸附。若发现转盘无反应,请检查 CSS 中 pointer-events 是否阻挡了触摸事件,或确认移动端视口设置是否正确。

程序定义了基础的双转盘 DOM 结构及 CSS 样式,JS 部分封装了 WheelHandler 类处理交互。输入为手指在屏幕上的滑动轨迹,代码通过累加位移计算当前角度,并在 touchend 事件中应用 transition 属性实现平滑的惯性减速或回弹效果。预期结果是转盘能跟随手指快速滑动并自然停止,或在释放后自动修正到预设的吸附点。

多转盘联动与状态同步

沿用上述 WheelHandler 实例,解决两个转盘独立旋转时的数据耦合问题。

当第一个转盘转动时,需通过回调或事件机制通知第二个转盘进行相应的数值映射或视觉反馈。例如,主转盘每转动一格,副转盘可能对应转动固定比例的角度。关键在于维护一个全局的状态对象,确保每次角度变更都同步更新两个转盘的 transform 值和内部计数变量,避免视觉不同步或数值溢出。

作者最新文章
相关文章
更多