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

首頁 > 編程 > JavaScript > 正文

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

2019-11-20 21:27:47
字體:
供稿:網(wǎng)友

js與jquery獲得頁面大小、滾動條位置、元素位置

復(fù)制代碼 代碼如下:

//頁面位置及窗口大小

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

獲取瀏覽器顯示區(qū)域的高度 : $(window).height();
獲取瀏覽器顯示區(qū)域的寬度 :$(window).width();
獲取頁面的文檔高度 :$(document).height();
獲取頁面的文檔寬度 :$(document).width();

獲取滾動條到頂部的垂直高度 :$(document).scrollTop();
獲取滾動條到左邊的垂直寬度 :$(document).scrollLeft();

計算元素位置和偏移量
offset方法是一個很有用的方法,它返回包裝集中第一個元素的偏移信息。默認(rèn)情況下是相對body的偏移信息。結(jié)果包含 top和left兩個屬性。
offset(options, results)
options.relativeTo  指定相對計 算偏移位置的祖先元素。這個元素應(yīng)該是relative或absolute定位。省略則相對body。
options.scroll  是否把 滾動條計算在內(nèi),默認(rèn)TRUE
options.padding  是否把padding計算在內(nèi),默認(rèn)false
options.margin   是否把margin計算在內(nèi),默認(rèn)true
options.border  是否把邊框計算在內(nèi),默認(rèn)true

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长垣县| 新民市| 东辽县| 松滋市| 潮州市| 石首市| 邻水| 商水县| 卢龙县| 开封县| 临泽县| 绥中县| 平远县| 新竹县| 潢川县| 马山县| 德令哈市| 莱州市| 定远县| 永春县| 巴林左旗| 永济市| 将乐县| 广南县| 安徽省| 漾濞| 石狮市| 益阳市| 米泉市| 虎林市| 澄迈县| 银川市| 工布江达县| 余庆县| 云龙县| 罗甸县| 舟曲县| 垫江县| 宽城| 常山县| 常山县|