jQuery .position() method retrieves the current position of an element relative to the offset parent

Syntax and Description

.position()

gets the current coordinates of the first element in the set of matched elements, relative to the offset parent. Its return value is an object containing the properties top and left.

.position() method retrieves the current position of an element relative to the offset parent. .offset() retrieves the current position relative to the document.

Get the top and left position


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--  w ww . j a  va  2 s  .  com-->
            $("*", document.body).click(function (e) {
                  var position= $(this).position();
                  e.stopPropagation();
                  alert(position.left);
                  alert(position.top);
           });
        });
    </script>
  </head>
  <body>
    <body>
       <b>Hello java2s.com java2s.co m</b>
    </body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities