From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../api/element/scrollintoviewifneeded/index.html | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 files/zh-cn/web/api/element/scrollintoviewifneeded/index.html (limited to 'files/zh-cn/web/api/element/scrollintoviewifneeded') diff --git a/files/zh-cn/web/api/element/scrollintoviewifneeded/index.html b/files/zh-cn/web/api/element/scrollintoviewifneeded/index.html new file mode 100644 index 0000000000..dd419a7d6d --- /dev/null +++ b/files/zh-cn/web/api/element/scrollintoviewifneeded/index.html @@ -0,0 +1,56 @@ +--- +title: Element.scrollIntoViewIfNeeded() +slug: Web/API/Element/scrollIntoViewIfNeeded +tags: + - API + - DOM + - 可视区域 + - 方法 + - 滚动 + - 非标准 +translation_of: Web/API/Element/scrollIntoViewIfNeeded +--- +
{{APIRef("DOM")}}{{Non-standard_header}}
+ +

Element.scrollIntoViewIfNeeded()方法用来将不在浏览器窗口的可见区域内的元素滚动到浏览器窗口的可见区域。 如果该元素已经在浏览器窗口的可见区域内,则不会发生滚动。 此方法是标准的Element.scrollIntoView()方法的专有变体。

+ +

语法

+ +
element.scrollIntoViewIfNeeded(); // 等同于element.scrollIntoViewIfNeeded(true)
+element.scrollIntoViewIfNeeded(true);
+element.scrollIntoViewIfNeeded(false); 
+ +

参数

+ +
+
opt_center
+
一个 {{jsxref("Boolean")}} 类型的值,默认为true: +
    +
  • 如果为true,则元素将在其所在滚动区的可视区域中居中对齐。
  • +
  • 如果为false,则元素将与其所在滚动区的可视区域最近的边缘对齐。 根据可见区域最靠近元素的哪个边缘,元素的顶部将与可见区域的顶部边缘对准,或者元素的底部边缘将与可见区域的底部边缘对准。
  • +
+
+
+ +

示例

+ +
var element = document.getElementById("child");
+
+element.scrollIntoViewIfNeeded();
+element.scrollIntoViewIfNeeded(true);
+element.scrollIntoViewIfNeeded(false);
+
+ +

规范

+ +

不属于任何规范,是一种WebKit专有的方法。

+ +

浏览器支持

+ +

{{Compat("api.Element.scrollIntoViewIfNeeded")}}

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf