How to use offset() Method in JQuery

In this article, I will explain use of offset () Method in JQuery.
  • 2308

JQuery CSS offset() Method

  • offset() method gets the current offset (position) for the selected elements,  in pixels.
  • offset() method returned object contains two Float properties, top and left.

Syntax

$(selector).offset()

Example

The following example shows how to offset() method in jquery.

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function () {

        $("button").click(function () {

            x = $("p").offset();

            alert("Left offset: " + x.left + " Top offset: " + x.top);

        });

    });

</script>

</head>

<body>

<h3>offset() method example</h3>

<p>First paragraph.</p>

<button>Click for display the offset coordinates of the p element</button>

</body>

</html>

 

Output

 

simple offset.jpg

Further Readings

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.