Gulir jendelaY
Contoh 1
Gulir konten sebesar 100 piksel, dan beri tahu scrollX dan scrollY:
window.scrollBy(100, 100);
alert(window.scrollX + window.scrollY);
Lebih banyak contoh di bawah ini.
Definisi dan Penggunaan
Properti scrollY
mengembalikan piksel yang telah digulir dokumen dari sudut kiri atas jendela.
Properti scrollY
ini hanya-baca.
Catatan
Harta scrollY
itu sama dengan
pageYOffset
harta.
Untuk kompatibilitas lintas-browser, gunakan window.pageYOffset alih-alih window.scrollY.
Lihat juga:
Sintaksis
window.scrollY
atau hanya:
scrollY
Nilai Kembali
Jenis | Keterangan |
Sebuah angka | Jumlah piksel yang digulir dokumen dari sudut kiri atas jendela. |
Lebih Banyak Contoh
Buat bilah navigasi lengket:
// 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.scrollY >= sticky) {
navbar.classList.add("sticky")
}
else {
navbar.classList.remove("sticky");
}
}
Dukungan Peramban
window.scrollY
didukung di semua browser:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |