Phần tử DOM HTML cuộn sang trái
Ví dụ
Lấy số pixel nội dung của "myDIV" được cuộn:
const element = document.getElementById("myDIV");
let x = elmnt.scrollLeft;
let y = elmnt.scrollTop;
Hãy tự mình thử »Cuộn nội dung của "myDIV" TO 50 pixel theo chiều ngang và 10 pixel theo chiều dọc:
const element = document.getElementById("myDIV");
element.scrollLeft = 50;
element.scrollTop = 10;
Hãy tự mình thử »Cuộn nội dung của "myDIV" BY 50 pixel theo chiều ngang và 10 pixel theo chiều dọc:
const element = document.getElementById("myDIV");
element.scrollLeft += 50;
element.scrollTop += 10;
Hãy tự mình thử »Thêm ví dụ dưới đây.
Sự miêu tả
Thuộc tính scrollLeft
đặt hoặc trả về số pixel mà nội dung của phần tử được cuộn theo chiều ngang.
Cú pháp
Trả về thuộc tính ScrollLeft:
element .scrollLeft
Đặt thuộc tính ScrollLeft:
element .scrollLeft = pixels
Giá trị tài sản
Value | Description |
pixels |
The number of pixels the element's content is scrolled horizontally. If the number is negative, the number is set to 0. If the element cannot be scrolled, the number is set to 0. If the number is greater than maximum allowed, the number is set to the maximum. |
Giá trị trả về
Kiểu | Sự miêu tả |
Con số | Số pixel nội dung của phần tử được cuộn theo chiều ngang. |
Thêm ví dụ
Ví dụ
Cuộn nội dung của <body> 30 pixel theo chiều ngang và 10 pixel theo chiều dọc:
const html = document.documentElement;
html.scrollLeft += 30;
html.scrollTop += 10;
Hãy tự mình thử »Hỗ trợ trình duyệt
element.scrollLeft
được hỗ trợ trong tất cả các trình duyệt:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |