Jquery provides a lot of methods to obtain the element position. If we have methods such as position and offset, I will introduce you to the element position acquisition method example on the page.
Jquery provides two methods to obtain the element position:
Position method:
Obtains the coordinates of the first element in the matching element set. The obtained element is equivalent to the offset of the nearest parent element with absolute or relative positioning. Usage:
The Code is as follows: |
Copy code |
$ ("# Target"). position (). left; $ ("# Target"). position (). top; |
Offset method:
Obtains the coordinates of the first element in the Matching Element Set, and obtains the offset position of the element relative to the document Object.
Usage:
The Code is as follows: |
Copy code |
$ ("# Target"). offset (). left; $ ("# Target"). offset (). top; |
1. Get the object (custom adjustment opens new window reference element)
The Code is as follows: |
Copy code |
Var obj = $ ("# oButton ");
|
The object obtained in the instance is the pop-up window button, so that the position of the created window will be adjusted based on the button, and the lower right corner of the button displayed in the new window will be displayed in the instance.
2. Obtain the position (offset () method) of the object element)
The Code is as follows: |
Copy code |
Var offset = obj. offset ();
|
Obtains the position of an object element, including the top and left of the element. The call method is offset. left and offset. top. The left and top positions of the current object are displayed.
3. Obtain the width () of the object element)
The Code is as follows: |
Copy code |
Var right = offset. left + obj. width ();
|
The instance obtains the position in the lower-right corner of the object and creates the left position of the new window.
4. Obtain the height () method of the object element)
The Code is as follows: |
Copy code |
Var down = offset. top + obj. height ();
|
The instance obtains the position at the bottom right corner of the object and creates the position at the top of the new window.