Jquery obtains the page element location instance

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.