Languages
[Edit]
EN

JavaScript - check for scrollTo() method got finished

10 points
Created by:
Hayley-Mooney
677

In this article, we would like to show how to detect when scrollTo() method finished execution in JavaScript.

There is not available event that signals when scrollTo() method ends execution, so there is needed to use some trick to solve the problem. In the example solution we provide reusable logic that checks state once per some time.

Example solution:

Where:

  • in SmoothScroller class constructor:
    • element represents handle to HTML element that is scrolled,
    • interval means how often state checking is done, e.g. 100 means once per 100ms,
    • chances means how many state failing checks are accepted, e.g. when something or someone tries to stop scrolling ro changes scrolling destination point,
    • tolerance was introduced to makes possible floating-point values usage by web browser when window has sets zoom,
  • in scroll() method:
    • x and y means scrolling destination point,
    • callback means function called when scrolling is done or canceled.

 

Practical example

Edit

 

See also

Edit
  1. JavaScript - scroll stop event

Alternative titles

  1. JavaScript - check whether the scrollTo() method has completed
  2. JavaScript - detect when scrollTo() method finished execution
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join