js與jquery獲得頁面大小、捲軸位置、元素位置
//頁面位置及視窗大小
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 = (scrW<winW) ? winW : scrW;
var pageH = (scrH<winH) ? winH : scrH;
return {PageW:pageW, PageH:pageH, WinW:winW, WinH:winH};
};
//捲軸位置
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
擷取瀏覽器顯示地區的高度 : $(window).height();
擷取瀏覽器顯示地區的寬度 :$(window).width();
擷取頁面的文檔高度 :$(document).height();
擷取頁面的文檔寬度 :$(document).width();
擷取捲軸到頂部的垂直高度 :$(document).scrollTop();
擷取捲軸到左邊的垂直寬度 :$(document).scrollLeft();
計算元素位置和位移量
offset方法是一個很有用的方法,它返回封裝集中第一個元素的位移資訊。預設情況下是相對body的位移資訊。結果包含 top和left兩個屬性。
offset(options, results)
options.relativeTo 指定相對計 算位移位置的祖先元素。這個元素應該是relative或absolute定位。省略則相對body。
options.scroll 是否把 捲軸計算在內,預設TRUE
options.padding 是否把padding計算在內,預設false
options.margin 是否把margin計算在內,預設true
options.border 是否把邊框計算在內,預設true