scrollTo function

void scrollTo(
  1. num? x,
  2. num? y, {
  3. bool smooth = true,
  4. int? delayMs,
  5. Object? scrollable,
})

Scrolls viewport to x,y.

  • If smooth is true will animate the scroll.
  • If delayMs >= 1 it will scroll after a Future.delayed. (value in milliseconds)
  • scrollable is the element to scroll. If null it will be the window or the body, identifying which one is scrolled.

Implementation

void scrollTo(num? x, num? y,
    {bool smooth = true, int? delayMs, Object? scrollable}) {
  if (delayMs != null && delayMs > 0) {
    _callAsync(
        delayMs, () => scrollTo(x, y, smooth: smooth, scrollable: scrollable));
    return;
  }

  scrollable = _resolveScrollable(scrollable);

  final params = {
    if (x != null) 'left': x.toInt(),
    if (y != null) 'top': y.toInt(),
    if (smooth) 'behavior': 'smooth',
  };

  if (scrollable is Window) {
    scrollable.scrollTo(params);
  } else if (scrollable is Element) {
    scrollable.scrollTo(params);
  } else {
    window.scrollTo(params);
  }
}