The
offset() method set or returns the offset coordinates for the selected
elements, relative to the document.
Syntax:
|
$(selector).offset()
|
Example:
|
<!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(){
var x = $("p").offset();
alert("Top: " + x.top + " Left: " + x.left);
});
});
</script>
</head>
<body>
<button>click here to Return the offset coordinates of the p element</button>
<p>This is a paragraph.</p>
</body>
</html>
|