国产探花免费观看_亚洲丰满少妇自慰呻吟_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>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 法库县| 泾川县| 明光市| 巴彦县| 藁城市| 石狮市| 田林县| 大宁县| 普安县| 米泉市| 阜新| 吐鲁番市| 彝良县| 天峨县| 抚松县| 天津市| 宁远县| 新巴尔虎右旗| 尉犁县| 会同县| 福清市| 镇远县| 育儿| 锡林郭勒盟| 兴安县| 共和县| 建水县| 新乡市| 寻乌县| 揭阳市| 友谊县| 二手房| 营山县| 惠安县| 惠水县| 阿坝| 石嘴山市| 西乌珠穆沁旗| 东乡县| 广东省| 延津县|