JQuery method set for getting element document size, offset, position, and scroll bar position _ jquery

Source: Internet
Author: User
In ajax, you often need to precisely locate the position of an element. In this case, you not only need to obtain attributes such as the size and position of the element. You also need to know the length and width of the page, browser, and scroll bar. Because of browser compatibility issues, it is quite painful to obtain these values using javascript. Fortunately, JQuery provides simple, elegant, and compatible solutions.
Obtain the width and height of the browser and page document

The Code is as follows:


// Obtain the height of the display area of the browser
$ (Window). height ();
// Obtain the width of the browser display area
$ (Window). width ();

// Obtain the document height of the page
$ (Document. body). height ();
// Obtain the document width on the page
$ (Document. body). width ();


Obtain the position of the scroll bar

The Code is as follows:


// Obtain the vertical height from the scroll bar to the top
$ (Document). scrollTop ();
// Obtain the vertical width from the scroll bar to the left
$ (Document). scrollLeft ();


Calculation position and offset
The offset method is a useful method that returns the offset information of the first element in the packaging set. By default, it is the offset information relative to the body. The result contains the top and left attributes.
Offset (options, results)
Options. relativeTo specifies the ancestor element relative to the calculated offset. This element should be located by relative or absolute. If this parameter is omitted, it is relative to the body.
Options. Does scroll include the scroll bar? The default value is TRUE.
Options. whether to include padding. The default value is false.
Options. Whether margin is included. The default value is true.
Options. border determines whether to include the border. The default value is true.
Related Article

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.