收集 scroll 事件

广告位招租
扫码页面底部二维码联系

scroll 事件是不支持冒泡的,那么怎【版权所有,侵权必究】【版权所有】唐霜 www.tangshuang.net么收集呢?当然是在捕获阶段进行收集了。

转载请注明出处:www.tangshuang.net【本文首发于唐霜的博客】【本文受版权保护】
document.addEventListener('scroll', (e) => {
  const target = e.target
  const isTop = Object.getPrototypeOf(target).constructor.name === 'HTMLDocument'
  const scrollLeft = isTop ? window.scrollX : target.scrollLeft
  const scrollTop = isTop ? window.scrollY : target.scrollTop
  console.log({ target, scrollLeft, scrollTop })
}, true)

这样就可以通过兼容方式收集到滚动事件的信【未经授权禁止转载】转载请注明出处:www.tangshuang.net息了。

【本文首发于唐霜的博客】【本文受版权保护】【访问 www.tangshuang.net 获取更多精彩内容】本文版权归作者所有,未经授权不得转载。