StaticcreateCreates a new instance of ScrollWatcher with the given ScrollWatcherConfig. It does not save it for future reuse.
Optionalconfig: ScrollWatcherConfigStaticfetchReturns the element that holds the main page content. By default it's
document.body but is overridden by
Settings.settings.mainScrollableElementSelector.
It will wait for the element to be available if not already.
StaticfetchReturns the scrollable element that holds the wrapper around the main page
content. By default it's document.scrollable (unless document.body is
actually scrollable, in which case it will be used) but it will be
different if Settings.settings.mainScrollableElementSelector is set.
It will wait for the element to be available if not already.
StaticreuseReturns an existing instance of ScrollWatcher with the given ScrollWatcherConfig, or creates a new one.
NOTE: It saves it for future reuse, so don't use this for temporary short-lived watchers.
Optionalconfig: ScrollWatcherConfigReadonlyfetchGet the scroll offset of the given scrollable. By default, it will waitForMeasureTime and so will be delayed by one frame.
Optionalscrollable: ScrollTargetOptionalrealtime: booleanIf true, it will not waitForMeasureTime. Use this only when doing realtime scroll-based animations as it may cause a forced layout.
LisnUsageError If the scrollable is invalid.
ReadonlyfetchReturns the current ScrollAction if any.
Optionalscrollable: ElementIf not given, it defaults to the main scrolling element
LisnUsageError If the scrollable is invalid.
ReadonlynoRemoves a previously added handler for trackScroll.
LisnUsageError If the scrollable is invalid.
ReadonlyoffRemoves a previously added handler.
LisnUsageError If the scrollable is invalid.
ReadonlyonCall the given handler whenever the given scrollable is scrolled.
Unless OnScrollOptions.skipInitial is true, the handler is also
called (almost) immediately with the latest scroll data. If a scroll has
not yet been observed on the scrollable and its scrollTop and
scrollLeft are 0, then the direction is Types.NoDirection and
the handler is only called if Types.NoDirection is part of the
supplied options.directions (or
options.directions is not given).
IMPORTANT: The same handler can not be added multiple times for the same scrollable, even if the options differ. If the handler has already been added for this scrollable, either using trackScroll or using onScroll, then it will be removed and re-added with the current options. So if previously it was also tracking content size changes using trackScroll, it will no longer do so.
LisnUsageError If the options are invalid.
ReadonlyscrollScrolls the given scrollable element to in the given direction.
LisnUsageError If the direction or options are invalid.
ReadonlyscrollScrolls the given scrollable element to the given to scrollable.
Returns null if there's an ongoing scroll that is not cancellable.
Note that if to is an element or a selector, then it must be a
descendant of the scrollable element.
If this is an element, then its top-left position is used as
the target coordinates. If it is a string, then it is treated
as a selector for an element using querySelector.
Optionaloptions: ScrollToOptionsOptionalaltOffset?: CoordinateOffsetOffset the target coordinates by the given amount(s) when altTarget is used.
OptionalaltTarget?: string | Element | TargetCoordinatesIf the scrolling element is already at the given coordinates (or strictly
speaking we allow for 5 pixels difference), then if altTarget is given,
this will become the target to scroll to.
Optionalduration?: numberThe duration of the scroll animation in milliseconds. If not given, it is instant.
Optionaloffset?: CoordinateOffsetOffset the target coordinates by the given amount(s).
E.g. if the final target coordinates are computed to be
{top: 100, left: 0} and you specify offset as {top: 10, left: 20}, it
will scroll to {top: 110, left: 20}.
Optionalscrollable?: ElementThe element that should be scrolled.
OptionaluserCanInterrupt?: booleanWhether a user attempt to scroll the target can interrupt this before it finishes.
OptionalweCanInterrupt?: booleanWhether another request to us to scroll the same target can interrupt this scroll before it finishes.
null if there's an ongoing scroll that is not cancellable,
otherwise a ScrollAction.
LisnUsageError If the "to" coordinates or options are invalid.
ReadonlystopCancels the ongoing scroll that's resulting from smooth scrolling triggered in the past. Does not interrupt or prevent further scrolling.
LisnUsageError If the scrollable is invalid.
ReadonlytrackThis everything that onScroll does plus more:
In addition to a scroll event, the handler is also called when either the
offset size or scroll (content) size of the scrollable changes as that
would affect its scrollTopFraction and scrollLeftFraction and possibly
the scrollTop and scrollLeft as well.
IMPORTANT: The same handler can not be added multiple times for the same scrollable, even if the options differ. If the handler has already been added for this scrollable, either using trackScroll or using onScroll, then it will be removed and re-added with the current options.
If handler is not given, then it defaults to an internal handler that
updates a set of CSS variables on the scrollable element's style:
If options.scrollable is not given,
or is null, window or document, the following CSS variables are
set on the root (html) element and represent the scroll of the
the main scrolling element:
--lisn-js--page-scroll-top--lisn-js--page-scroll-top-fraction--lisn-js--page-scroll-left--lisn-js--page-scroll-left-fraction--lisn-js--page-scroll-width--lisn-js--page-scroll-heightOtherwise, the following variables are set on the scrollable itself, and represent its scroll offset:
--lisn-js--scroll-top--lisn-js--scroll-top-fraction--lisn-js--scroll-left--lisn-js--scroll-left-fraction--lisn-js--scroll-width--lisn-js--scroll-heightLisnUsageError If the options are invalid.
ScrollWatcher listens for scroll events in any direction.
It manages registered callbacks globally and reuses event listeners for more efficient performance.