jquery 手册
Ajax
属性(Attributes)
回调对象(Callbacks ..
核心(Core)
CSS
数据(Data)
延迟对象(Deferred o ..
尺寸(Dimensions)
效果(Effects)
事件(Events)
表单(Forms)
文档操作(Manipulati ..
杂项(Miscellaneou ..
位置(Offset)
插件编写(Plugin Aut ..
属性(Properties)
选择器(Selectors)
遍历(Traversing)
工具(Utilities)

Returns: ObjectObject .offset( )

Description: Get the current coordinates of the first element in the set of matched elements, relative to the document.
  • version added: 1.2
  • .offset( )

The .offset() method allows us to retrieve the current position of an element relative to the document. Contrast this with .position(), which retrieves the current position relative to the offset parent. When positioning a new element on top of an existing one for global manipulation (in particular, for implementing drag-and-drop), .offset() is the more useful.

.offset() returns an object containing the properties top and left.

Note: jQuery does not support getting the offset coordinates of hidden elements or accounting for borders, margins, or padding set on the body element.

Examples:

Example:

Access the offset of the second paragraph:
<!DOCTYPE html>
<html>
<head>
  <script src="../jquery.min.js"></script>
  <style>p { margin-left:10px; }</style>
</head>
<body>
  <p>Hello</p><p>2nd Paragraph</p>
  <script>var p = $("p:last");
var offset = p.offset();
p.html( "left: " + offset.left + ", top: " + offset.top );</script>
</body>
</html>

Demo:

Example:

Click to see the offset.
<!DOCTYPE html>
<html>
<head>
  <script src="../jquery.min.js"></script>
  <style>p { margin-left:10px; color:blue; width:200px; 
    cursor:pointer; }
span { color:red; cursor:pointer; }
div.abs { width:50px; height:50px; position:absolute;
          left:220px; top:35px; background-color:green; 
          cursor:pointer; }</style>
</head>
<body>
  <div id="result">Click an element.</div>
<p>
  This is the best way to <span>find</span> an offset.
</p>

<div class="abs">
</div>
  
  <script>$("*", document.body).click(function (e) {
  var offset = $(this).offset();
  e.stopPropagation();
  $("#result").text(this.tagName + " coords ( " + offset.left + ", " +
                                  offset.top + " )");
});</script>
</body>
</html>

Demo:

Returns: jQueryjQuery .offset( Function function(index, coords) )

Description: Set the current coordinates of every element in the set of matched elements, relative to the document.
  • version added: 1.4
  • .offset( Object coordinates )
  • coordinates An object containing the properties top and left, which are integers indicating the new top and left coordinates for the elements.
  • version added: 1.4
  • .offset( Function function(index, coords) )
  • function(index, coords) A function to return the coordinates to set. Receives the index of the element in the collection as the first argument and the current coordinates as the second argument. The function should return an object with the new top and left properties.

The .offset() setter method allows us to reposition an element. The element's position is specified relative to the document. If the element's position style property is currently static, it will be set to relative to allow for this repositioning.

Examples:

Example:

Set the offset of the second paragraph:
<!DOCTYPE html>
<html>
<head>
  <script src="../jquery.min.js"></script>
  <style>p { margin-left:10px; }</style>
</head>
<body>
  <p>Hello</p><p>2nd Paragraph</p>
  <script>$("p:last").offset({ top: 10, left: 30 });</script>
</body>
</html>

Demo: