Snippet · 运行时核心

按一下键盘,幻灯片就跑起来了

// runtime.js — keyboard-driven deck
function go(n) {
  n = Math.max(0, Math.min(total - 1, n));
  slides.forEach((s, i) => {
    s.classList.toggle('is-active', i === n);
  });
  idx = n;
  barFill.style.width = ((n + 1) / total * 100) + '%';
  history.replaceState(null, '', '#/' + (n + 1));
}

document.addEventListener('keydown', (e) => {
  if (e.key === 'ArrowRight' || e.key === ' ') go(idx + 1);
  if (e.key === 'ArrowLeft') go(idx - 1);
  if (e.key === 't') cycleTheme();
});