tadam logo
Did you find an error in the text?
Select this with mouse and press
Ctrl + Enter
Xhtml.co.il Check Spelling
Orphus system

position()

.position()

Returns: Object

Description: Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.

.position()

version added: 1.2

The .position() method allows us to retrieve the current position of an element relative to the offset parent. Contrast this with .offset(), which retrieves the current position relative to the document. When positioning a new element near another one and within the same containing DOM element, .position() is the more useful.

Returns an object containing the properties top and left.

Examples

Access the position of the second paragraph:
var p = $("p:first");
var position = p.position();
$("p:last").text( "left: " + position.left + ", top: " + position.top );
The output of the code above will be:
Full source:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>  
  <script>
  $(document).ready(function(){
var p = $("p:first");
var position = p.position();
$("p:last").text( "left: " + position.left + ", top: " + position.top );
  });
  </script>
  <style>
  div { padding: 15px;}
  p { margin-left:10px; }
  </style>
</head>
<body>
  <div><p>Hello</p></div><p></p>
</body>
</html>
Was this information helpful?
   

Comments