PHP前端开发

scrollY属性在JavaScript中的作用是什么?

百变鹏仔 4周前 (09-22) #HTML
文章标签 属性

scrollY属性在JavaScript中与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>