scrollIntoView 方法

void scrollIntoView(
  1. [ScrollAlignment? alignment]
)

将此元素滚动到视图中。

一次只能指定一个对齐选项。

如果没有指定任何选项,则会尝试滚动所需的最少距离以将元素滚动到视图中。

注意,目前 alignCenter 只在 WebKit 平台上受支持。如果指定了 alignCenter 但不受支持,则会回退到 alignTop。

另请参阅

实现

void scrollIntoView([ScrollAlignment? alignment]) {
  var hasScrollIntoViewIfNeeded = true;
  hasScrollIntoViewIfNeeded =
      JS('bool', '!!(#.scrollIntoViewIfNeeded)', this);
  if (alignment == ScrollAlignment.TOP) {
    this._scrollIntoView(true);
  } else if (alignment == ScrollAlignment.BOTTOM) {
    this._scrollIntoView(false);
  } else if (hasScrollIntoViewIfNeeded) {
    // TODO(srujzs): This method shouldn't be calling out to
    // `scrollIntoViewIfNeeded`. Remove this and make `scrollIntoView` match
    // the browser definition. If you intend to use `scrollIntoViewIfNeeded`,
    // use the `Element.scrollIntoViewIfNeeded` method.
    if (alignment == ScrollAlignment.CENTER) {
      this.scrollIntoViewIfNeeded(true);
    } else {
      this.scrollIntoViewIfNeeded();
    }
  } else {
    this._scrollIntoView();
  }
}