Prototype - viewportOffset() Method



This method returns the X/Y coordinates of element relative to the viewport. Returned positions will be absolute.

NOTE − All values are returned as numbers, only although they are expressed in pixels.

Syntax

element.viewportOffset();

Return Value

An array of numbers [Left, Top].

Example

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

Output

prototype_element_object.htm
Advertisements