หน้าต่างหน้าXOffset
ตัวอย่าง 1
เลื่อนเนื้อหาไป 100 พิกเซล และแจ้งเตือน pageXOffset และ pageYOffset:
window.scrollBy(100, 100);
alert(window.pageXOffset + window.pageYOffset);
ตัวอย่างเพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
คุณสมบัติpageXOffset
ส่งคืนพิกเซลที่เอกสารเลื่อนจากมุมซ้ายบนของหน้าต่าง
ทรัพย์สินมี ค่าpageXOffset
เท่ากับscrollX
ทรัพย์สิน
คุณสมบัติpageXOffset
เป็นแบบอ่านอย่างเดียว
ไวยากรณ์
window.pageXOffset
หรือเพียงแค่:
pageXOffset
คืนมูลค่า
พิมพ์ | คำอธิบาย |
หมายเลข | จำนวนพิกเซลที่เอกสารเลื่อนจากมุมซ้ายบนของหน้าต่าง |
ตัวอย่างเพิ่มเติม
สร้างแถบการนำทางที่ติดหนึบ:
// Get the navbar
const navbar = document.getElementById("navbar");
// Get the offset position of the navbar
const sticky = navbar.offsetTop;
// Add the sticky class to the navbar when you reach its scroll position. Remove the sticky class when you leave the scroll position.
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
}
else {
navbar.classList.remove("sticky");
}
}
รองรับเบราว์เซอร์
window.pageXOffset
รองรับในทุกเบราว์เซอร์:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |