I can't find the difference between doing:
window.onscroll = () => console.log("scroll")
and this:
window.addEventListener('scroll', () => console.log("scroll"))
except for browser compatibility which both seems to be unsupported in most IE versions !
is it just a syntax difference? it seems that it is straightforward to remove the handler using removeEventListener
, but I'm assuming window.onscroll = null
has similar effect.
am I missing anything?
The main differences is that there can only be one onscroll
. Any additional onscroll
s you add will override the first, whereas you can have as many .addEventListener('scroll', ()=>{})
as you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With