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.