ドキュメント

CSS/position

From jQuery JavaScript Library

Jump to: navigation, search

« Back to CSS

position( )

Gets the top and left position of an element relative to its offset parent.
The returned object contains two Integer properties, top and left. For accurate calculations make sure to use pixel values for margins, borders and padding. This method only works with visible elements.
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 );

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<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>

NameType