Phần tử HTML DOM cuộnIntoView
Ví dụ
Cuộn phần tử có id="content" vào vùng hiển thị của cửa sổ trình duyệt:
const element = document.getElementById("content");
element.scrollIntoView();
Hãy tự mình thử »Thêm ví dụ dưới đây.
Sự miêu tả
Phương thức scrollIntoView()
cuộn một phần tử vào vùng hiển thị của cửa sổ trình duyệt.
Cú pháp
element .scrollIntoView( align )
Thông số
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. |
Giá trị trả về
KHÔNG CÓ |
Thêm ví dụ
Ví dụ
Cuộn lên đầu hoặc xuống cuối phần tử:
const element = document.getElementById("content");
function scrollToTop()
{
element.scrollIntoView(true);
}
function scrollToBottom() {
element.scrollIntoView(false);
}
Hãy tự mình thử »Hỗ trợ trình duyệt
element.scrollIntoView()
được hỗ trợ trong tất cả các trình duyệt:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |