From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/scrolltooptions/index.html | 72 ++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 files/zh-cn/web/api/scrolltooptions/index.html (limited to 'files/zh-cn/web/api/scrolltooptions') diff --git a/files/zh-cn/web/api/scrolltooptions/index.html b/files/zh-cn/web/api/scrolltooptions/index.html new file mode 100644 index 0000000000..53593c4232 --- /dev/null +++ b/files/zh-cn/web/api/scrolltooptions/index.html @@ -0,0 +1,72 @@ +--- +title: ScrollToOptions +slug: Web/API/ScrollToOptions +translation_of: Web/API/ScrollToOptions +--- +
{{ APIRef("CSSOM View") }}
+ +

CSSOM View 规范的 ScrollToOptions 字典(dictionary)当中的属性用于指定一个元素应该滚动到哪里,以及滚动是否应该平滑。

+ +

一个 ScrollToOptions 字典可以作为参数提供给下面的方法:

+ + + +

属性

+ +
+
{{domxref("ScrollToOptions.top")}}
+
指定 window 或元素 Y 轴方向滚动的像素数。
+
{{domxref("ScrollToOptions.left")}}
+
指定 window 或元素 X 轴方向滚动的像素数。
+
{{domxref("ScrollToOptions.behavior")}}
+
指定滚动是否应该平滑进行,还是立即跳到指定位置。该属性实际上定义在 ScrollOptions 字典上,它通过 ScrollToOptions 实现。
+
+ +

示例

+ +

在我们的 scrolltooptions 示例中(在线查看 ),包含一个表单,允许用户输入三个值——两个数值表示 left 和 top 属性(即沿 X 和 Y 轴方向滚动后的位置),以及一个表示是否开启平滑滚动的复选框。

+ +

当提交表单时,会运行事件监听器,该事件监听器会把输入的值写入 ScrollToOptions 字典,然后传入 {{domxref("Window.ScrollTo()")}} 方法,并调用:

+ +
form.addEventListener('submit', (e) => {
+  e.preventDefault();
+  scrollOptions = {
+    left: leftInput.value,
+    top: topInput.value,
+    behavior: scrollInput.checked ? 'smooth' : 'auto'
+  }
+
+  window.scrollTo(scrollOptions);
+});
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('CSSOM View', '#dictdef-scrolltooptions', 'ScrollToOptions')}}{{Spec2('CSSOM View')}}
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.ScrollToOptions", 10)}}

+
-- cgit v1.2.3-54-g00ecf