Description:
The jquery .position() method Get the current coordinates of the first element for the matched elements, relative to the offset parent.ย This method does not accept any arguments.
Syntax
- $(selector).position()
When to use?
The .position() method allows us to retrieve the current position of an element relative to the offset parent. It will be more useful if we need toย place a new element near another one within the same containerย . It returns an object containing the top and left properties as shown below
Example:ย
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script> <style> .defaultClass { border: 5px solid #eed5b7; padding: 10px; width: 50px; height: 50px; background-color: red; display:inline-block; } </style> <script type="text/javascript"> $(document).ready(function () { $('.btnClass').click(function () { var divBoxDetail = $("#divBoxDetails"); var position = divBoxDetail.position(); divBoxDetail.text("left: " + Math.round(position.left) + ", top: " + Math.round(position.top)); }) }); </script> </head> <body> <button class="btnClass" id="addMethod">GEt Position</button><br/> <div id="divbox1" class="defaultClass"> </div> <br /><br /> <div id="divBoxDetails"></div> </body> </html> |
Output:ย
Leave A Comment