中文
危险操作确认
Click & feedback · Vanilla JS + CSS

Hold to confirm

长按填充确认

Press and hold a button to fill its progress bar from left to right; releasing early cancels.

Also searched as: 长按按钮填充确认, 按住进度条走满才触发, 防误触长按删除, 长按确认操作

AI Markdown (.md)
Placement
Button
Mood
Crisp, Dramatic
Trigger
点击
Site types
工具和应用, 个人网站

How it works & accessibility

Uses WAAPI on a left-anchored fill layer during pointer or Space/Enter hold; releasing before completion cancels and retracts the bar while completing the hold dispatches a confirm event, with an input lock preventing multi-source conflicts.

Best for: Button (点击).

Reduced motion: 保留长按计时防误触,关闭按压微缩与平滑回退过渡,进度直接切换并触发确认

Mobile & touch: 禁用长按弹出系统文本选择菜单(user-select: none / touch-action: manipulation),按钮高度不少于 44px

Key parameters

ParameterKeyDefaultRangeNotes
长按时长duration900ms——
缓动easinglinear——

Ready-to-paste AI prompt

Add “Hold to confirm” to the button in my project: Uses WAAPI on a left-anchored fill layer during pointer or Space/Enter hold; releasing before completion cancels and retracts the bar while completing the hold dispatches a confirm event, with an input lock preventing multi-source conflicts.
Parameters: duration: 900ms, easing: linear.
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/hold-to-confirm.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 长按填充确认(Hold to confirm)样式,配合 hold-to-confirm.js。 */
.htc-btn {
  position: relative;
  display: inline-grid;
  place-items: center;
  min-height: 44px;
  padding: 0 20px;
  border: 1.5px solid var(--htc-border, currentColor);
  border-radius: 10px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 700;
  overflow: hidden;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: transform 0.14s ease;
}

.htc-btn.is-holding {
  transform: scale(0.97);
}

.htc-fill {
  position: absolute;
  inset: 0;
  background: var(--htc-fill, currentColor);
  opacity: 0.18;
  transform-origin: left center;
  transform: scaleX(0);
  pointer-events: none;
}

.htc-btn.is-confirmed .htc-fill {
  opacity: 1;
  transform: scaleX(1);
}

.htc-btn.is-confirmed .htc-label {
  color: var(--htc-done-ink, #ffffff);
}

.htc-label {
  position: relative;
  z-index: 1;
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  .htc-btn:not(.htc-force) {
    transition: none;
  }
  .htc-btn:not(.htc-force).is-holding {
    transform: none;
  }
}
JavaScript
// 长按填充确认(Hold to confirm)参考实现。
// 用法:<button class="htc-btn"><span class="htc-fill"></span><span class="htc-label">长按确认</span></button>
// 调 const ctl = holdToConfirm(btn, { duration: 900, easing: 'linear', onConfirm: () => {} }); 销毁调 ctl.destroy()。

export function holdToConfirm(btn, {
  duration = 900,
  easing = 'linear',
  onConfirm = null,
  ignoreReducedMotion = false,
} = {}) {
  if (!(btn instanceof Element)) return null;
  const fill = btn.querySelector('.htc-fill');
  if (!fill) return null;

  const dur = Number(duration) || 900;
  const easeStr = String(easing);
  const reduceQuery = typeof matchMedia === 'function' ? matchMedia('(prefers-reduced-motion: reduce)') : null;
  const isReduced = () => Boolean(reduceQuery?.matches && !ignoreReducedMotion);

  btn.classList.add('htc-btn');
  if (ignoreReducedMotion) btn.classList.add('htc-force');

  let activeMode = null; // 'pointer' | 'key' | 'program' | null
  let activePointerId = null;
  let confirmed = false;
  let destroyed = false;
  let fillAnim = null;
  let holdTimer = 0;

  const clearTimers = () => {
    if (holdTimer) { clearTimeout(holdTimer); holdTimer = 0; }
    if (fillAnim) { fillAnim.cancel(); fillAnim = null; }
  };

  const commitConfirm = () => {
    if (destroyed || confirmed) return;
    clearTimers();
    activeMode = null;
    activePointerId = null;
    confirmed = true;
    btn.classList.remove('is-holding');
    btn.classList.add('is-confirmed');
    fill.style.transform = 'scaleX(1)';
    if (typeof onConfirm === 'function') onConfirm();
    if (destroyed) return;
    btn.dispatchEvent(new CustomEvent('confirm', { bubbles: true }));
  };

  const startHold = (mode, pointerId = null) => {
    if (destroyed || confirmed || activeMode !== null) return;
    activeMode = mode;
    activePointerId = pointerId;
    btn.classList.add('is-holding');
    clearTimers();
    if (isReduced()) {
      fill.style.transform = 'scaleX(0.35)';
      holdTimer = setTimeout(commitConfirm, dur);
    } else {
      fillAnim = fill.animate(
        [{ transform: 'scaleX(0)' }, { transform: 'scaleX(1)' }],
        { duration: dur, easing: easeStr, fill: 'forwards' },
      );
      fillAnim.finished.then(() => {
        if (!destroyed && activeMode === mode) commitConfirm();
      }).catch(() => undefined);
    }
  };

  const cancelHold = (mode) => {
    if (destroyed || confirmed || (mode && activeMode !== mode)) return;
    activeMode = null;
    activePointerId = null;
    btn.classList.remove('is-holding');
    clearTimers();
    if (!isReduced()) {
      const current = getComputedStyle(fill).transform;
      const retract = fill.animate(
        [{ transform: current && current !== 'none' ? current : 'scaleX(0)' }, { transform: 'scaleX(0)' }],
        { duration: 180, easing: 'ease-out', fill: 'none' },
      );
      retract.finished.then(() => { if (!destroyed && !confirmed) fill.style.transform = 'scaleX(0)'; }).catch(() => undefined);
    } else {
      fill.style.transform = 'scaleX(0)';
    }
  };

  const onPointerDown = (ev) => {
    if (ev.pointerType === 'mouse' && ev.button !== 0) return;
    startHold('pointer', ev.pointerId);
  };
  const onPointerUp = (ev) => {
    if (activeMode === 'pointer' && ev.pointerId === activePointerId) cancelHold('pointer');
  };
  const onPointerLeave = () => { if (activeMode === 'pointer') cancelHold('pointer'); };
  const onKeyDown = (ev) => {
    if (ev.repeat) return;
    if (ev.key === ' ' || ev.key === 'Enter') { ev.preventDefault(); startHold('key'); }
  };
  const onKeyUp = (ev) => {
    if (ev.key === ' ' || ev.key === 'Enter') cancelHold('key');
  };
  const onBlur = () => { cancelHold(null); };

  btn.addEventListener('pointerdown', onPointerDown);
  btn.addEventListener('pointerup', onPointerUp);
  btn.addEventListener('pointercancel', onPointerUp);
  btn.addEventListener('pointerleave', onPointerLeave);
  btn.addEventListener('keydown', onKeyDown);
  btn.addEventListener('keyup', onKeyUp);
  btn.addEventListener('blur', onBlur);

  return {
    start() { startHold('program'); },
    cancel() { cancelHold(null); },
    reset() {
      if (destroyed) return;
      clearTimers();
      activeMode = null;
      activePointerId = null;
      confirmed = false;
      btn.classList.remove('is-holding', 'is-confirmed');
      fill.style.transform = 'scaleX(0)';
    },
    isConfirmed: () => confirmed,
    destroy() {
      if (destroyed) return;
      destroyed = true;
      clearTimers();
      btn.removeEventListener('pointerdown', onPointerDown);
      btn.removeEventListener('pointerup', onPointerUp);
      btn.removeEventListener('pointercancel', onPointerUp);
      btn.removeEventListener('pointerleave', onPointerLeave);
      btn.removeEventListener('keydown', onKeyDown);
      btn.removeEventListener('keyup', onKeyUp);
      btn.removeEventListener('blur', onBlur);
      btn.classList.remove('htc-force', 'is-holding', 'is-confirmed');
      fill.style.transform = '';
    },
  };
}
HTML
<button type="button" class="htc-btn" id="delete-btn">
  <span class="htc-fill" aria-hidden="true"></span>
  <span class="htc-label">长按删除</span>
</button>

<script type="module">
  import { holdToConfirm } from './hold-to-confirm.js';
  const btn = document.getElementById('delete-btn');
  holdToConfirm(btn, {
    duration: 900,
    onConfirm: () => { btn.querySelector('.htc-label').textContent = '已确认删除'; },
  });
</script>

More in Click & feedback

Browse all 86 effects →

Shown once per release · Always available in changelog