--- title: Element.scrollIntoView() slug: Web/API/Element/scrollIntoView translation_of: Web/API/Element/scrollIntoView ---
element.scrollIntoView(); // 等同于element.scrollIntoView(true) element.scrollIntoView(alignToTop); // Boolean型参数 element.scrollIntoView(scrollIntoViewOptions); // Object型参数
alignToTop
{{optional_inline}}true
,元素的顶端将和其所在滚动区的可视区域的顶端对齐。相应的 scrollIntoViewOptions: {block: "start", inline: "nearest"}
。这是这个参数的默认值。false
,元素的底端将和其所在滚动区的可视区域的底端对齐。相应的scrollIntoViewOptions: {block: "end", inline: "nearest"}
。scrollIntoViewOptions
{{optional_inline}} {{experimental_inline}}behavior
{{optional_inline}}"auto"
或 "smooth"
之一。默认为 "auto"
。block
{{optional_inline}}"start"
, "center"
, "end"
, 或 "nearest"
之一。默认为 "start"
。inline
{{optional_inline}}"start"
, "center"
, "end"
, 或 "nearest"
之一。默认为 "nearest"
。var element = document.getElementById("box");
element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({block: "end"});
element.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});
取决于其它元素的布局情况,此元素可能不会完全滚动到顶端或底端。
Specification | Status | Comment |
---|---|---|
{{SpecName("CSSOM View", "#dom-element-scrollintoview", "Element.scrollIntoView()")}} | {{Spec2("CSSOM View")}} | Initial definition |
{{Compat("api.Element.scrollIntoView")}}