国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JS和JQUERY獲取頁面大小,滾動條位置,元素位置(示例代碼)

2024-05-06 15:57:27
字體:
來源:轉載
供稿:網友
這篇文章主要是對JS和JQUERY獲取頁面大小,滾動條位置,元素位置的示例代碼進行了介紹。需要的朋友可以過來參考下,希望對大家有所幫助

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 剑河县| 南召县| 汝阳县| 南投市| 关岭| 淳安县| 藁城市| 连州市| 镇原县| 金秀| 镶黄旗| 特克斯县| 高碑店市| 方正县| 铜鼓县| 金坛市| 敦煌市| 徐闻县| 巴彦县| 宜城市| 云霄县| 镇远县| 鸡泽县| 遂昌县| 黄浦区| 民乐县| 深州市| 舒城县| 潜江市| 云和县| 巨野县| 合川市| 南部县| 项城市| 辽宁省| 永平县| 饶阳县| 蒲城县| 闽侯县| 鲁山县| 连州市|