Thuộc tính HTML DOM scrollLeft
Thí dụ
Nhận số pixel mà nội dung của phần tử <div> được cuộn theo chiều ngang và chiều dọc:
var elmnt = document.getElementById("myDIV");
var x = elmnt.scrollLeft;
var y = elmnt.scrollTop;
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
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.
Mẹo: Sử dụng thuộc tính scrollTop để đặt hoặc trả về số pixel mà nội dung của phần tử được cuộn theo chiều dọc.
Mẹo: Để thêm thanh cuộn vào một phần tử, hãy sử dụng thuộc tính phần bổ sung CSS .
Hỗ trợ trình duyệt
Property | |||||
---|---|---|---|---|---|
scrollLeft | Yes | Yes | Yes | Yes | Yes |
Cú pháp
Trả lại 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 |
Specifies the number of pixels the element's content is scrolled horizontally. Special notes:
|
Chi tiết kỹ thuật
Giá trị trả lại: | Số, đại diện cho số pixel mà nội dung của phần tử đã được cuộn theo chiều ngang |
---|
Các ví dụ khác
Thí dụ
Cuộn nội dung của phần tử <div> TO 50 pixel theo chiều ngang và 10 pixel theo chiều dọc:
var elmnt = document.getElementById("myDIV");
elmnt.scrollLeft = 50;
elmnt.scrollTop = 10;
Thí dụ
Cuộn nội dung của phần tử <div> BY 50 pixel theo chiều ngang và 10 pixel theo chiều dọc:
var elmnt = document.getElementById("myDIV");
elmnt.scrollLeft += 50;
elmnt.scrollTop += 10;
Thí dụ
Cuộn nội dung của <body> 30 pixel theo chiều ngang và 10 pixel theo chiều dọc:
var body = document.body; // Safari
var html = document.documentElement; //
Chrome, Firefox, IE and Opera places the overflow at the <html> level, unless else is specified. Therefore, we use the
documentElement property for these browsers
body.scrollLeft += 30;
body.scrollTop += 10;
html.scrollLeft += 30;
html.scrollTop += 10;