Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

90 lines (88 sloc) 4.387 kB
<?xml version="1.0"?>
<entries>
<entry type="method" name="offset" return="Object">
<title>.offset()</title>
<signature>
<added>1.2</added>
</signature>
<desc>Get the current coordinates of the first element in the set of matched elements, relative to the document.</desc>
<longdesc>
<p>The <code>.offset()</code> method allows us to retrieve the current position of an element <em>relative to the document</em>. Contrast this with <code>.position()</code>, which retrieves the current position <em>relative to the offset parent</em>. When positioning a new element on top of an existing one for global manipulation (in particular, for implementing drag-and-drop), <code>.offset()</code> is the more useful.</p>
<p><code>.offset()</code> returns an object containing the properties <code>top</code> and <code>left</code>.</p>
<blockquote>
<p><strong>Note:</strong> jQuery does not support getting the offset coordinates of hidden elements or accounting for borders, margins, or padding set on the body element.</p>
<p>While it is possible to get the coordinates of elements with <code>visibility:hidden</code> set, <code>display:none</code> is excluded from the rendering tree and thus has a position that is undefined.</p>
</blockquote>
</longdesc>
<example>
<desc>Access the offset of the second paragraph:</desc>
<code><![CDATA[var p = $("p:last");
var offset = p.offset();
p.html( "left: " + offset.left + ", top: " + offset.top );]]></code>
<css><![CDATA[
p { margin-left:10px; }
]]></css>
<html><![CDATA[<p>Hello</p><p>2nd Paragraph</p>]]></html>
</example>
<example>
<desc>Click to see the offset.</desc>
<code><![CDATA[
$("*", document.body).click(function (e) {
var offset = $(this).offset();
e.stopPropagation();
$("#result").text(this.tagName + " coords ( " + offset.left + ", " +
offset.top + " )");
});
]]></code>
<css><![CDATA[
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; }
]]></css>
<html><![CDATA[<div id="result">Click an element.</div>
<p>
This is the best way to <span>find</span> an offset.
</p>
<div class="abs">
</div>
]]></html>
</example>
<category slug="css"/>
<category slug="offset"/>
<category slug="manipulation/style-properties"/>
<category slug="version/1.2"/>
<category slug="version/1.4"/>
</entry>
<entry type="method" name="offset" return="jQuery">
<signature>
<added>1.4</added>
<argument name="coordinates" type="PlainObject">
<desc>An object containing the properties <code>top</code> and <code>left</code>, which are integers indicating the new top and left coordinates for the elements.</desc>
</argument>
</signature>
<signature>
<added>1.4</added>
<argument name="function(index, coords)" type="Function">
<desc>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 <code>top</code> and <code>left</code> properties.</desc>
</argument>
</signature>
<desc>Set the current coordinates of every element in the set of matched elements, relative to the document.</desc>
<longdesc>
<p>The <code>.offset()</code> setter method allows us to reposition an element. The element's position is specified <em>relative to the document</em>. If the element's <code>position</code> style property is currently <code>static</code>, it will be set to <code>relative</code> to allow for this repositioning.</p>
</longdesc>
<example>
<desc>Set the offset of the second paragraph:</desc>
<code><![CDATA[$("p:last").offset({ top: 10, left: 30 });]]></code>
<css><![CDATA[p { margin-left:10px; } ]]></css>
<html><![CDATA[<p>Hello</p><p>2nd Paragraph</p>]]></html>
</example>
<category slug="css"/>
<category slug="offset"/>
<category slug="manipulation/style-properties"/>
<category slug="version/1.2"/>
<category slug="version/1.4"/>
</entry>
</entries>
Jump to Line
Something went wrong with that request. Please try again.