Scroll the element with id="content" into the visible area of the browser window:
const element = document.getElementById("content");
element.scrollIntoView();
Try it Yourself »
More examples below.
The scrollIntoView()
method scrolls an element into the visible area of the browser window.
element.scrollIntoView(
align)
Parameter | Description |
align | Optional. A boolean that indicates the type of the align: true - the top of the element will be aligned to the top of the visible area of the scrollable ancestor false - the bottom of the element will be aligned to the bottom of the visible area of the scrollable ancestor. If omitted, it will scroll to the top of the element. Note: Depending on the layout of other elements, some elements may not be scrolled completely to the top or to the bottom. |
NONE |
Scroll to the top or to the bottom of an element:
const element = document.getElementById("content");
function scrollToTop() {
element.scrollIntoView(true);
}
function scrollToBottom() {
element.scrollIntoView(false);
}
Try it Yourself »
element.scrollIntoView()
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!