Javascript examples for jQuery Method and Property:offset
The offset() method sets or gets the offset coordinates for the selected elements, relative to the document.
Parameter | Require | Description | Value |
---|---|---|---|
{top:value,left:value} | Required when setting the offset. | sets the top and left coordinates in pixels. | Possible values: Name/Value pairs, like {top:100,left:100} |
function(index,currentoffset) | Optional. | sets a function that returns an object containing the top and left coordinates | function |
The following code shows how to set the offset of an element.
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").offset($("span").offset()); });//from www . j a va2s. co m }); </script> </head> <body> <p>This is a paragraph.</p> <button>Set the offset coordinates of the p element equal to the span element</button> <span style="position:absolute;left:100px;top:150px;">This is a span</span> </body> </html>