JS與JQUERY擷取頁面大小、捲軸位置、元素位置

來源:互聯網
上載者:User

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.