Home >Web Front-end >JS Tutorial >JS and JQUERY get page size, scroll bar position, element position (sample code)_jquery

JS and JQUERY get page size, scroll bar position, element position (sample code)_jquery

WBOY
WBOYOriginal
2016-05-16 17:09:04906browse

JS and jquery get the page size, scroll bar position, and element position

Copy the code The code is as follows:

//Page position and window size

function GetPageSize() {
var scrW, scrH;
if(window.innerHeight && window.scrollMaxY)
{ // Mozilla
scrW = window.innerWidth window.scrollMaxX;
scrH = window.innerHeight window.scrollMaxY;
}
else if(document.body.scrollHeight > document.body.offsetHeight)
{ // all but IE Mac
scrW = document. body.scrollWidth;
scrH = document.body.scrollHeight;
} else if(document.body)
{ // IE Mac
scrW = document.body.offsetWidth;
scrH = document.body.offsetHeight;
}
var winW, winH;
if(window.innerHeight)
{ // all except IE  
winW = window.innerWidth;
winH = window.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight)
{ // IE 6 Strict Mode
winW = document.documentElement.clientWidth;
winH = document.documentElement .clientHeight;
} else if (document.body) { // other
winW = document.body.clientWidth;
winH = document.body.clientHeight;
} // for small pages with total size less then the viewport
var pageW = (scrWvar pageH = (scrHreturn {PageW:pageW, PageH:pageH , WinW:winW, WinH:winH};

};

//Scroll bar position
function GetPageScroll()
{
var x, y; if(window.pageYOffset)
{ / / all except IE
y = window.pageYOffset;
x = window.pageXOffset;
} else if(document.documentElement && document.documentElement.scrollTop)
{ // IE 6 Strict
y = document.documentElement.scrollTop;
x = document.documentElement.scrollLeft;
} else if(document.body) { // all other IE
y = document.body.scrollTop;
x = document.body.scrollLeft;
}
return {X:x, Y:y};

}


jquery

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).height();
Get the document width of the page: $(document).width();

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 element 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.relativeTo Specifies 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