jQuery | offset() with Examples - GeeksforGeeks

30 Aug, 2021
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. 
html
<!DOCTYPE html><html>    <head>        <title>The offset Method</title>        <script src=        "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">        </script>                 <!-- jQuery code to show the working 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>
Output: 
Example 2: 
html
<!DOCTYPE html><html>   <head>       <title>The offset Method</title>       <script src=       "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">       </script>               <!-- jQuery code to show the working 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>
Output:  
sweetyty
jQuery-HTML/CSS
JavaScript
JQuery
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
JQuery | Set the value of an input text field
Form validation using jQuery
How to change selected value of a drop-down list using jQuery?
How to change the background color after clicking the button in JavaScript ?
How to fetch data from JSON file and display in HTML table using jQuery ?