Cửa sổ cuộnBy()
Ví dụ
Cuộn tài liệu 100px theo chiều ngang:
window.scrollBy(100, 0);
Hãy tự mình thử »Cuộn tài liệu 100px theo chiều dọc:
window.scrollBy(0, 100);
Hãy tự mình thử »Thêm ví dụ dưới đây.
Sự miêu tả
Phương thức scrollBy()
cuộn tài liệu theo số pixel được chỉ định.
Ghi chú
Để phương thức scrollBy()
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ả về
KHÔNG CÓ |
Thêm ví dụ
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>
Hãy tự mình thử »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ãy tự mình thử »Hỗ trợ trình duyệt
scrollBy()
đượ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 |