原型 - viewportOffset() 方法



此方法返回相對於視口的元素的 X/Y 座標。返回的位置將是絕對位置。

注意 - 雖然都用畫素表示,但所有值均以數字形式返回。

語法

element.viewportOffset();

返回值

數字 [Left, Top] 陣列。

例項

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var coordinates = $('visible').viewportOffset();
            alert("Distance from the Left : " +  coordinates[0] );
            alert("Distance from the Top  : " +  coordinates[1] );
         }
      </script>
   </head>

   <body>
      <div id = "visible" style = "position:absolute;left:50px; top:50px;">
         This is visible division 50 pixels down from the top.
      </div>
      <input type = "button" value = "Click" onclick = "showResult();"/>
   </body>
</html>

輸出

prototype_element_object.htm
廣告
© . All rights reserved.