jQuery | offset() with Examples

The offset() method is an inbuilt method in jQuery which is used to set or returns the offset coordinates of the selected element.

Syntax:

  • $(selector).offset()

    Parameters: Parameter does not required.

  • $(selector).offset({top:value, left:value})

    Parameters: The parameter is required when set the offset.

  • $(selector).offset( function(index, offset) )

    Parameters: This method set the offset using function. The parameter used in this method is optional. The index parameter is used to return the position of set element and offset return the coordinate of selected element.

Return Value: This method returns the co-ordinate of matched elements.



Below examples illustrate the offset() method in jQuery:

Example 1: In the code below this will return the co-ordinate of the first matched element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>The offset Method</title>
        <script src=
        </script>
          
        <!-- jQuery code to show the woirking of this method -->
        <script>
            $(document).ready(function() {
                $("button").click(function() {
                    var Geek = $("p").offset();
                    alert("Top coordinate: " + Geek.top + 
                             "   Left Coordinate: " + Geek.left);
                });
            });
        </script>
        <style>
            div {
                width: 60%;
                min-height: 150px;
                padding: 20px;
                font-size: 25px;
                border: 2px solid green;
                font-weight: bold;
                color:green;
            }
        </style>
    </head>
       
    <body>
        <!-- Click on paragraph -->
        <div>
            <p>Welcome to GeeksforGeeks!</p>
            <button>click Here!</button>
        </div>
    </body>
</html>

chevron_right


Output:
offset

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   <head>
       <title>The offset Method</title>
       <script src=
       </script>
         
       <!-- jQuery code to show the woirking of this method -->
       <script>
            $(document).ready(function() {
                $("button").click(function() {
                    $("p").offset({top: 100, left: 140});
             });
         });
      </script>
      <style>
         div{
             width: 300px;
             min-height: 100px;
             color:green;
             font-weight: bold;
             padding:20px;
             font-size: 25px;
             border: 2px solid green;
         }
      </style>
   </head>
   <body>
      <div>
         <!-- Click on paragraph -->
         <p>Welcome to GeeksforGeeks!</p>
         <button>Click Here!</button>
      </div>
   </body>
</html>

chevron_right


Output:
offset method



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.