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
//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
/ /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