萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 實現雙擊自動滾動:requestAnimationFrame 平滑滾動、速度控制、鍵盤/滑鼠停止、移動端適配

雙擊頁面自動滾動閱讀是長文閱讀、直播彈幕、代碼審查的實用功能。本文使用 requestAnimationFrame 實現平滑匀速滾動,支援速度調節、多種停止觸發、移動端長按觸發、無障礙語意。

核心實現

class AutoScroller {
  constructor(options = {}) {
    this.speed = options.speed || 1; // px/frame
    this.acceleration = options.acceleration || 0; // px/frame²
    this.direction = options.direction || 'down'; // 'down' | 'up'
    this._rafId = null;
    this._lastTime = 0;
    this._boundStop = this.stop.bind(this);
    this._bind();
  }

  _bind() {
    document.addEventListener('dblclick', this._toggle.bind(this));
    document.addEventListener('keydown', this._onKey.bind(this));
    document.addEventListener('mousedown', this._boundStop);
    document.addEventListener('touchstart', this._boundStop, { passive: true });
    document.addEventListener('wheel', this._boundStop, { passive: true });
  }

  _toggle(e) {
    if (e.target.closest('a,button,input,textarea,select,[contenteditable]')) return; // 排除互動元素
    this.isRunning ? this.stop() : this.start();
  }

  start() {
    if (this.isRunning) return;
    this.isRunning = true;
    this._lastTime = performance.now();
    this._animate();
  }

  stop() {
    if (!this.isRunning) return;
    this.isRunning = false;
    cancelAnimationFrame(this._rafId);
  }

  _animate() {
    if (!this.isRunning) return;
    const now = performance.now();
    const dt = (now - this._lastTime) / 16.67; // 幀時間歸一化
    this._lastTime = now;

    const scrollAmount = (this.speed + this.acceleration * dt) * (this.direction === 'down' ? 1 : -1);
    window.scrollBy(0, scrollAmount);

    // 到達底部/頂部自動停止
    if ((this.direction === 'down' && window.scrollY + window.innerHeight >= document.documentElement.scrollHeight) ||
        (this.direction === 'up' && window.scrollY <= 0)) {
      this.stop();
      return;
    }

    this._rafId = requestAnimationFrame(this._animate.bind(this));
  }

  _onKey(e) {
    if (e.key === 'Escape' || e.key === ' ') this.stop(); // ESC 或空格停止
    if (e.key === 'ArrowUp') { this.direction = 'up'; this.speed = Math.abs(this.speed); }
    if (e.key === 'ArrowDown') { this.direction = 'down'; this.speed = Math.abs(this.speed); }
    if (e.key === '+' || e.key === '=') this.speed += 0.5;
    if (e.key === '-') this.speed = Math.max(0.5, this.speed - 0.5);
  }
}

// 初始化
new AutoScroller({ speed: 2, acceleration: 0.01 });

移動端適配:長按觸發

let pressTimer;
document.addEventListener('touchstart', e => {
  if (e.target.closest('a,button,input,textarea,select,[contenteditable]')) return;
  pressTimer = setTimeout(() => autoScroller._toggle(e), 500);
}, { passive: true });
document.addEventListener('touchend', () => clearTimeout(pressTimer), { passive: true });
document.addEventListener('touchmove', () => clearTimeout(pressTimer), { passive: true });

無障礙與 UX 建議

  • 提供可見的控制面板:顯示當前速度、方向、暫停/繼續按鈕。
  • 宣告 aria-live="polite" 通知螢幕閱讀器狀態變化。
  • 避免在表單、編輯器、互動組件頁面全局啟用,改為用戶主動開啟。
copyright © 萬盛學電腦網 all rights reserved