scrollY属性在JavaScript中起什么作用?

JavaScript中的scrollY属性与pageYoffset属性相同。如果要获取文档从窗口左上角滚动到的像素,请对垂直像素使用scrollY属性。

示例

您可以尝试运行以下代码,以了解如何在JavaScript中使用scrollY属性。

<!DOCTYPE html>
<html>
   <head>
      <style>
         div {
            background-color: yellow;
            height: 1500px;
            width: 1500px;
         }
      </style>
   </head>

   <body>
      <script>
         function scrollFunc() {
            window.scrollBy(200, 200);
            alert("Vertical: " + window.scrollY);
         }
      </script>
      <button onclick="scrollFunc()" style="position:fixed;">Scroll</button>
      <br><br><br>
     <div>
     </div>
   </body>
</html>