收集 scroll 事件

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

scroll 事件是不支持冒泡的,那么怎转载请注明出处:www.tangshuang.net【关注微信公众号:wwwtangshuangnet】么收集呢?当然是在捕获阶段进行收集了。

【原创内容,转载请注明出处】【本文受版权保护】【本文首发于唐霜的博客】转载请注明出处: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息了。

著作权归作者所有,禁止商业用途转载。【原创不易,请尊重版权】【原创内容,转载请注明出处】著作权归作者所有,禁止商业用途转载。