Home >Web Front-end >JS Tutorial >JQuery method collection to obtain element document size, offset and position and scroll bar position_jquery

JQuery method collection to obtain element document size, offset and position and scroll bar position_jquery

WBOY
WBOYOriginal
2016-05-16 18:36:361337browse

Because of browser compatibility issues, it is a quite painful process to use JavaScript to obtain these values. Fortunately, JQuery provides a simple, elegant, and compatible solution.
Get the width and height of the browser and page document

Copy the code The code is as follows:

//Get the height of the browser display area
$(window).height();
//Get the width of the browser display area
$(window).width();

//Get the document height of the page
$(document.body).height();
//Get the document width of the page
$(document.body).width();

Get the position of the scroll bar
Copy the code The code is as follows:

/ /Get the vertical height of the scroll bar from the top
$(document).scrollTop();
//Get the vertical width of the scroll bar from the left
$(document).scrollLeft();

Calculate position and offset
The offset method is a very useful method, which returns the offset information of the first element in the packed set. By default, it is the offset information relative to the body. The result contains two attributes: top and left.
offset(options, results)
options.relativeToSpecifies the ancestor element relative to the calculated offset position. This element should be positioned relative or absolutely. If omitted, it is relative to body.
options.scroll Whether to include the scroll bar, the default is TRUE
options.padding Whether to include the padding, the default is false
options.margin Whether to include the margin, the default is true
options .border Whether to include the border, default true
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn