Showing posts with label jQuery offset() method. Show all posts
Showing posts with label jQuery offset() method. Show all posts

jQuery offset() method

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

Syntex:


$(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>