blurScrollTop.client.ts 542 B

1234567891011121314151617181920
  1. export default defineNuxtPlugin(() => {
  2. if (!import.meta.client) return
  3. const onBlur = (e: FocusEvent) => {
  4. const target = e.target as HTMLElement | null
  5. if (!target) return
  6. const tag = target.tagName
  7. if (tag !== 'INPUT' && tag !== 'TEXTAREA') return
  8. window.setTimeout(() => {
  9. window.scrollTo(0, 0)
  10. document.body.scrollTop = 0
  11. document.documentElement.scrollTop = 0
  12. }, 100)
  13. }
  14. // `blur` does not bubble; use capture to listen globally.
  15. document.addEventListener('blur', onBlur, true)
  16. })