offset() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:offset

Description

The offset() method sets or gets the offset coordinates for the selected elements, relative to the document.

Parameter

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
  • index - Returns the index position of the element in the set
  • currentoffset - Returns the current coordinates of the selected element

The following code shows how to set the offset of an element.

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials