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

首頁 > 開發 > JS > 正文

獲得網頁窗口實際大小的javascript代碼實例

2024-09-06 12:41:08
字體:
來源:轉載
供稿:網友

    在做web2.0風格的彈出窗口的時候,經常需要把背景變暗和屏蔽。這時就需要得到一個網頁的寬高數據。這里就會有很多細節和兼容性問題。經過長期摸索,現提供以下函數,希望對大家有用。這個函數的具體作用是獲得網頁的高度和寬度,如果網頁沒有窗口可見區域高的時候則取窗口的可見區域高度和寬度。返回值為一個對象 ,例如 { width:123, height:345}。現在兼容幾乎所有的瀏覽器。


    JavaScript代碼:

function get_page_size()
{
    var re = {};
    if (document.documentElement && document.documentElement.clientHeight)
    {
        var doc = document.documentElement;
        re.width = (doc.clientWidth>doc.scrollWidth)?doc.clientWidth-1:doc.scrollWidth;
        re.height = (doc.clientHeight>doc.scrollHeight)?doc.clientHeight:doc.scrollHeight;
    }
    else
    {
        var doc = document.body;
        re.width = (window.innerWidth>doc.scrollWidth)?window.innerWidth:doc.scrollWidth;
        re.height = (window.innerHeight>doc.scrollHeight)?window.innerHeight:doc.scrollHeight;
    }
    return re;
}


904 viewed 3 comment(s)

    代碼實例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>獲取窗口大小</title>
<script type="text/JavaScript">
function getPageSize()
{
    var re = {};
    if (document.documentElement && document.documentElement.clientHeight)
    {
        var doc = document.documentElement;
        re.width = (doc.clientWidth>doc.scrollWidth)?doc.clientWidth-1:doc.scrollWidth;
        re.height = (doc.clientHeight>doc.scrollHeight)?doc.clientHeight:doc.scrollHeight;
    }
    else
    {
        var doc = document.body;
        re.width = (window.innerWidth>doc.scrollWidth)?window.innerWidth:doc.scrollWidth;
        re.height = (window.innerHeight>doc.scrollHeight)?window.innerHeight:doc.scrollHeight;
    }
    //結果輸出至兩個文本框
    document.form1.availHeight.value= re.width;
    document.form1.availWidth.value= re.height;
    return re;
}
</script>
</head>
<body>
<form action="#" method="get" name="form1" id="form1">
  瀏覽器窗口 的 實際高度:
  <input type="text" name="availHeight" size="4"/>
  <br />
  瀏覽器窗口 的 實際寬度:
  <input type="text" name="availWidth" size="4"/>
  <br />
</form>
<script type="text/JavaScript">
getPageSize();
window.onresize=getPageSize;
</script>
</body>
</html>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五指山市| 沙田区| 柏乡县| 罗江县| 贡嘎县| 民丰县| 始兴县| 都安| 鸡西市| 额尔古纳市| 八宿县| 澳门| 涡阳县| 永春县| 万山特区| 长顺县| 铜山县| 彰化市| 平安县| 南宁市| 杨浦区| 长垣县| 柞水县| 布拖县| 昌平区| 闽清县| 永定县| 卓尼县| 乌审旗| 阿城市| 上高县| 宝清县| 噶尔县| 永昌县| 迁安市| 万山特区| 英吉沙县| 台前县| 莱西市| 县级市| 金阳县|