Element.scrollIntoView() - Web APIs 编辑
The
Element
interface's scrollIntoView()
method scrolls the element's parent container such that the element on which scrollIntoView()
is called is visible to the userSyntax
element.scrollIntoView(); element.scrollIntoView(alignToTop); // Boolean parameter element.scrollIntoView(scrollIntoViewOptions); // Object parameter
Parameters
alignToTop
Optional- Is a
Boolean
value:- If
true
, the top of the element will be aligned to the top of the visible area of the scrollable ancestor. Corresponds toscrollIntoViewOptions: {block: "start", inline: "nearest"}
. This is the default value. - If
false
, the bottom of the element will be aligned to the bottom of the visible area of the scrollable ancestor. Corresponds toscrollIntoViewOptions: {block: "end", inline: "nearest"}
.
- If
scrollIntoViewOptions
Optional- Is an Object with the following properties:
behavior
Optional- Defines the transition animation.
One ofauto
orsmooth
. Defaults toauto
. block
Optional- Defines vertical alignment.
One ofstart
,center
,end
, ornearest
. Defaults tostart
. inline
Optional- Defines horizontal alignment.
One ofstart
,center
,end
, ornearest
. Defaults tonearest
.
Example
var element = document.getElementById("box");
element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({block: "end"});
element.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});
Notes
The element may not be scrolled completely to the top or bottom depending on the layout of other elements.
Specifications
Specification | Status | Comment |
---|---|---|
CSS Object Model (CSSOM) View Module The definition of 'Element.scrollIntoView()' in that specification. | Working Draft | Initial definition |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论