Is a scroll event guaranteed when scrolling programmatically?

In Chrome, if I have an onscroll handler:

 document.body.onscroll = function () {console.log('foo')} 

... and I scroll through the document programmatically:

 document.body.scrollTop = 7 

... then the onscroll handler onscroll .

Can I depend on this behavior in browsers, or will someone only run the handler to scroll caused by user action? Is it indicated anywhere?

+5
source share
1 answer

I still don't know that (if anything), the spec says this, but I have now tested and confirmed that the handler works in the latest versions:

  • Chrome (desktop)
  • Chrome (mobile)
  • Firefox
  • Edge
  • Safari (desktop)

If there is a platform that you want to test, open https://jsfiddle.net/14nckcw1/4/embedded/result/ and wait; if you see a warning after 1 second, then software scrolling starts the onscroll handler on this platform. I don’t even know that this is not so.

0
source

Source: https://habr.com/ru/post/1262379/


All Articles