Prototype - positionedOffset() 方法



此方法返回元素相对其最近定位祖先的偏移。它计算元素及其所有父元素的累积 offsetLeftoffsetTop,直到到达位置为静态的元素。

注意 − 请注意,虽然所有值以像素表示,但只返回数字。

语法

element.positionedOffset();

返回值

返回元素相对其最近定位祖先的偏移。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var arr = $('container2').positionedOffset();
            alert("offsetLeft : " + arr[0] );
            alert("offsetTop : " + arr[1] );
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br />
      
      <div id = "container1">
         <div id = "element" 
            style = "position:absolute; top: 30px; left: 20px; 
            border:1px solid red;">
            This is the first Element Box
         </div>
      </div>

      <div id = "container2">
         <div id = "element" 
            style = "top: 30px; left: 20px; 
            border:1px solid red;">
            This is the second Element Box
         </div>
      </div>
      <br />
      
      <input type = "button" value = "Click" onclick = "showResult();"/>
   </body>
</html>

输出

prototype_element_object.htm
广告