หน้าต่าง scrollBy()
ตัวอย่าง
เลื่อนเอกสาร 100px ในแนวนอน:
window.scrollBy(100, 0);
เลื่อนเอกสาร 100px ในแนวตั้ง:
window.scrollBy(0, 100);
ตัวอย่างเพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
วิธี การscrollBy()
เลื่อนเอกสารตามจำนวนพิกเซลที่ระบุ
บันทึก
สำหรับscrollBy()
วิธีการทำงาน เอกสารจะต้องมีขนาดใหญ่กว่าหน้าจอ และต้องมองเห็นแถบเลื่อนได้
ดูสิ่งนี้ด้วย:
วิธี การscrollTo()
ไวยากรณ์
window.scrollBy(x, y)
หรือเพียงแค่:
scrollBy(x, y)
พารามิเตอร์
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. |
คืนมูลค่า
ไม่มี |
ตัวอย่างเพิ่มเติม
เลื่อนเอกสารขึ้นและลง:
<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>
เลื่อนเอกสารไปทางขวาและทางซ้าย:
<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>
รองรับเบราว์เซอร์
scrollBy()
รองรับในทุกเบราว์เซอร์:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |