Prototype - cumulativeOffset() 方法

此方法返回元素相对于文档左上角的偏移量。

此方法返回一个数组,其中包含元素的 offsetLeft 和 offsetTop。

请注意,所有值都以数字形式返回,尽管它们以像素表示。

语法

element.cumulativeOffset();

返回值

两个数字的数组 [offset left, offset top]。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function getOffset() {
            var firstElement = $('firstDiv');
            var arr = firstElement.cumulativeOffset();
            alert ( "Offset Left: " +arr[0]+ " Offset Top : " +arr[0] );
         }
      </script>
   </head>

   <body>
      <p>Click getOffset button to see the result.</p>
      <div id = "firstDiv">
         <p>This is first paragraph</p> 
      </div>
      <br />
      
      <input type ="button" value = "getOffset" onclick = "getOffset();"/>
   </body>
</html>

输出

prototype_element_object.html