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

原创
ithorizon 5个月前 (10-19) 阅读数 43 #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>

以上就是scrollY属性在JavaScript中的作用是什么?的详细内容,更多请关注IT视界其它相关文章!



热门