Prototype-element-viewportoffset

提供:Dev Guides
移動先:案内検索

プロトタイプ-viewportOffset()メソッド

このメソッドは、ビューポートに相対的な要素のX/Y座標を返します。 返される位置は絶対になります。

-すべての値は数値として返されますが、ピクセルで表されます。

構文

element.viewportOffset();

戻り値

数字の配列[左、上]。

<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>

出力