1 line
3.8 KiB
JSON
1 line
3.8 KiB
JSON
{"name":"offset","type":"method","title":".offset()","deprecated":null,"removed":null,"desc":"Get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document.","categories":["css","offset","manipulation/style-properties","version/1.2","version/1.4"],"entries":[{"return":"Object","signatures":{"added":"1.2"},"examples":[{"desc":"Access the offset of the second paragraph:","code":"\nvar p = $( \"p:last\" );\nvar offset = p.offset();\np.html( \"left: \" + offset.left + \", top: \" + offset.top );\n","css":"\n p {\n margin-left: 10px;\n }\n","html":"\n<p>Hello</p><p>2nd Paragraph</p>\n"},{"desc":"Click to see the offset.","code":"\n$( \"*\", document.body ).click(function( event ) {\n var offset = $( this ).offset();\n event.stopPropagation();\n $( \"#result\" ).text( this.tagName +\n \" coords ( \" + offset.left + \", \" + offset.top + \" )\" );\n});\n","css":"\n p {\n margin-left: 10px;\n color: blue;\n width: 200px;\n cursor: pointer;\n }\n span {\n color: red;\n cursor: pointer;\n }\n div.abs {\n width: 50px;\n height: 50px;\n position: absolute;\n left: 220px;\n top: 35px;\n background-color: green;\n cursor: pointer;\n }\n","html":"\n<div id=\"result\">Click an element.</div>\n<p>\n This is the best way to <span>find</span> an offset.\n</p>\n<div class=\"abs\">\n</div>\n"}],"desc":"Get the current coordinates of the first element in the set of matched elements, relative to the document.","longdesc":"\n <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 more useful.</p>\n <p><code>.offset()</code> returns an object containing the properties <code>top</code> and <code>left</code>.</p>\n <div class=\"warning\">\n <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>\n <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>\n </div>\n "},{"return":"jQuery","signatures":[{"added":"1.4","argument":{"desc":"An object containing the properties <code>top</code> and <code>left</code>, which are numbers indicating the new top and left coordinates for the elements.","name":"coordinates","type":"PlainObject"}},{"added":"1.4","argument":{"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.","name":"function(index, coords)","type":"Function"}}],"examples":{"desc":"Set the offset of the second paragraph:","code":"\n$( \"p:last\" ).offset({ top: 10, left: 30 });\n","css":"\n p {\n margin-left: 10px;\n }\n","html":"\n<p>Hello</p><p>2nd Paragraph</p>\n"},"desc":"Set the current coordinates of every element in the set of matched elements, relative to the document.","longdesc":"\n <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>\n "}]} |