jQuery offset() Method
jQuery offset() method sets or returns the current coordinates of the selected elements, relative to the document.
The offset() method is get the current coordinates of the first selected element or set the coordinates of every selected elements.
Syntax
Here is a syntax for offset() method that return the offset coordinates
$(selector).offset();
Set the offset coordinates
$(selector).offset( coordinates );
Parameter | Type | Description |
---|---|---|
coordinates | Object | Optional. Specifies the object containing the properties top and left . Which are numbers indicating coordinates for the elements. |
Example: Get offset
$(document).ready(function() {
$("button").click(function(){
Page = $("p.param").offset();
$("span").text("Left offset: " + Page.left + " Top offset: " + Page.top);
});
});
Example: Set offset
$(document).ready(function() {
$("button").click(function(){
$("p.param").offset({left: 20, top: 170});
});
});