将 id="content" 的元素滚动到浏览器窗口的可见区域:
const element = document.getElementById("content");
element.scrollIntoView();
亲自试一试 »
下面有更多示例。
这个scrollIntoView()
方法将元素滚动到浏览器窗口的可见区域。
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. |
没有任何 |
滚动到元素的顶部或底部:
const element = document.getElementById("content");
function scrollToTop() {
element.scrollIntoView(true);
}
function scrollToBottom() {
element.scrollIntoView(false);
}
亲自试一试 »
element.scrollIntoView()
所有浏览器都支持:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!