Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Vai trò của thuộc tính scrollY trong JavaScript?

Thuộc tính scrollY (tương đương pageYOffset) trả về số pixel đã cuộn theo chiều dọc từ góc trên bên trái của cửa sổ.

Ví dụ

<!DOCTYPE html>
<html>
  <head>
    <style>
      div { background-color: yellow; height: 1500px; width: 1500px; }
    </style>
  </head>
  <body>
    <script>
      function scrollFunc() {
        window.scrollBy(200, 200);
        alert("Dọc: " + window.scrollY);
      }
    </script>
    <button onclick="scrollFunc()" style="position:fixed;">Cuộn</button>
    <br><br><br>
    <div></div>
  </body>
</html>