Cuộn cửa sổBy ()
Thí dụ
Cuộn tài liệu theo chiều ngang 100px:
window.scrollBy(100, 0);
Cuộn tài liệu 100px theo chiều dọc:
window.scrollBy(0, 100);
Thêm ví dụ bên dưới.
Định nghĩa và Cách sử dụng
Phương scrollBy()
thức cuộn tài liệu theo số pixel được chỉ định.
Ghi chú
Để scrollBy()
phương pháp hoạt động, tài liệu phải lớn hơn màn hình và thanh cuộn phải hiển thị.
Xem thêm:
Cú pháp
window.scrollBy(x, y)
hoặc chỉ:
scrollBy(x, y)
Thông số
Parameter | Description |
x | Required. Number of pixels to scroll (horizontally). Positive values scroll to the right, negative values to the left. |
y | Required. Number ofpixels to scroll (vertically). Positive values scroll down, negative values scroll up. |
Giá trị trả lại
KHÔNG AI |
Các ví dụ khác
Cuộn tài liệu lên và xuống:
<button onclick="scrollWin(0, 50)">Scroll down</button>
<button onclick="scrollWin(0, -50)">Scroll up</button>
<script>
function scrollWin(x, y) {
window.scrollBy(x, y);
}
</script>
Cuộn tài liệu sang phải và sang trái:
<button onclick="scrollWin(100, 0)">Scroll right</button>
<button onclick="scrollWin(-100, 0)">Scroll left</button>
<script>
function scrollWin(x, y) {
window.scrollBy(x, y);
}
</script>
Hỗ trợ trình duyệt
scrollBy()
được hỗ trợ trong tất cả các trình duyệt:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |