Phương pháp scrollIntoView HTML DOM
Thí 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:
var elmnt = document.getElementById("content");
elmnt.scrollIntoView();
Thêm các ví dụ "Hãy tự mình thử" bên dưới.
Định nghĩa và Cách sử dụng
Phương thức scrollIntoView () cuộn phần tử được chỉ định vào vùng hiển thị của cửa sổ trình duyệt.
Hỗ trợ trình duyệt
Các số trong bảng chỉ định phiên bản trình duyệt đầu tiên hỗ trợ đầy đủ phương pháp này.
Method | |||||
---|---|---|---|---|---|
scrollIntoView | 28.0 | 8.0 | 1.7 | 5.0 | 38.0 |
Cú pháp
element.scrollIntoView(alignTo)
Thông số
Parameter | Type | Description |
---|---|---|
alignTo | Boolean | Optional. A boolean value 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. |
Chi tiết kỹ thuật
Giá trị trả lại: | Không có giá trị trả lại |
---|---|
Phiên bản DOM | Thực nghiệm |
Các ví dụ khác
Thí dụ
Cuộn lên đầu hoặc xuống cuối phần tử:
var elmnt = document.getElementById("content");
function scrollToTop()
{
elmnt.scrollIntoView(true); // Top
}
function scrollToBottom() {
elmnt.scrollIntoView(false); // Bottom
}