有时候,我们可能需要象新版的Yahoo邮箱一样,让一些数据显示块可以根据页面大小进行自适应。事实并不难,但是如果单纯用CSS控制的话,往往不能完全实现我们要的效果,这时候需要借助javascript,Javascript可以让我我们实现司空见怪的自适应页面,在iSunXoft的HR开源项目就应用到. 实现原理:
获取IE显示屏幕的宽高.确定哪些对象是绝对位置是固定的,那些是不固定的,哪些长宽是固定,然后象WIN FORM程序一样,根所软件界面的大小,进行设计相关对象的长宽,绝对位置等属性,但是在WEB上程序没有办法实时检测到窗口大小变化,只能用让实现自适应的方法通过SetTimeout 函数进行隔时递归调用.
下面是摘自iSunXoft Hr开源项目SysForm.aspx实现的方法.
注:$("")是有变化的元素.
var h;TrPLinux联盟
var w;TrPLinux联盟
function resize()TrPLinux联盟
{ TrPLinux联盟
TrPLinux联盟
var he = document.body.offsetHeight;TrPLinux联盟
var wi = document.body.offsetWidth;TrPLinux联盟
if($("DataTable").style.display.toLowerCase()==""||$("DataTable").style.display.toLowerCase()=="inline")TrPLinux联盟
{TrPLinux联盟
if (h==he&&w==wi)TrPLinux联盟
{TrPLinux联盟
if($("leftMenu").style.display.toLowerCase() == "none" )TrPLinux联盟
{TrPLinux联盟
$("DivDataList").style.width = wi - 30;TrPLinux联盟
}TrPLinux联盟
elseTrPLinux联盟
{TrPLinux联盟
$("DivDataList").style.width = wi - 223;TrPLinux联盟
} TrPLinux联盟
setTimeout("resize()",1000);TrPLinux联盟
return;TrPLinux联盟
}TrPLinux联盟
h = he;TrPLinux联盟
w = wi;TrPLinux联盟
TrPLinux联盟
if (he>100)TrPLinux联盟
{TrPLinux联盟
$("DivDataList").style.height = he - 172;TrPLinux联盟
}TrPLinux联盟
if(wi>200)TrPLinux联盟
{TrPLinux联盟
$("DivDataList").style.width = wi - 223;TrPLinux联盟
if($("leftMenu").style.display.toLowerCase() == "none" )TrPLinux联盟
{TrPLinux联盟
$("DivDataList").style.width = wi - 30;TrPLinux联盟
}TrPLinux联盟
}TrPLinux联盟
}
if($("DataEmpWidows").style.display.toLowerCase()=="inline"||$("DataEmpWidows").style.display.toLowerCase()=="")TrPLinux联盟
{TrPLinux联盟
if (h==he&&w==wi)TrPLinux联盟
{TrPLinux联盟
if($("leftMenu").style.display.toLowerCase() == "none" )TrPLinux联盟
{TrPLinux联盟
$("DataEmpWidows").style.width = wi - 30;TrPLinux联盟
}TrPLinux联盟
elseTrPLinux联盟
{TrPLinux联盟
$("DataEmpWidows").style.width = wi - 223;TrPLinux联盟
} TrPLinux联盟
}TrPLinux联盟
h = he;TrPLinux联盟
w = wi;TrPLinux联盟
if (he>150)TrPLinux联盟
{TrPLinux联盟
$("DataEmpWidows").style.height = he - 132;TrPLinux联盟
}TrPLinux联盟
if(wi>200)TrPLinux联盟
{TrPLinux联盟
$("DataEmpWidows").style.width = wi - 223;TrPLinux联盟
if($("leftMenu").style.display.toLowerCase() == "none" )TrPLinux联盟
{TrPLinux联盟
$("DataEmpWidows").style.width = wi - 30;TrPLinux联盟
}TrPLinux联盟
}TrPLinux联盟
TrPLinux联盟
}TrPLinux联盟
if(typeof($("EipWindows")) != "undefined")TrPLinux联盟
{TrPLinux联盟
//if($("EipWindows").style.display.toLowerCase()=="inline")TrPLinux联盟
//{TrPLinux联盟
// if (h==he&&w==wi)TrPLinux联盟
// {TrPLinux联盟
// if($("leftMenu").style.display.toLowerCase() == "none" )TrPLinux联盟
// {TrPLinux联盟
// $("EipWindows").style.width = wi - 30;TrPLinux联盟
// }TrPLinux联盟
// elseTrPLinux联盟
// {TrPLinux联盟
// $("EipWindows").style.width = wi - 223;TrPLinux联盟
// } TrPLinux联盟
// }TrPLinux联盟
// h = he;TrPLinux联盟
// w = wi;TrPLinux联盟
// if (he>150)TrPLinux联盟
// {TrPLinux联盟
// $("EipWindows").style.height = he - 132;TrPLinux联盟
// }TrPLinux联盟
// if(wi>200)TrPLinux联盟
// {TrPLinux联盟
// $("EipWindows").style.width = wi - 223;TrPLinux联盟
// if($("leftMenu").style.display.toLowerCase() == "none" )TrPLinux联盟
// {TrPLinux联盟
// $("EipWindows").style.width = wi - 30;TrPLinux联盟
// }TrPLinux联盟
// } TrPLinux联盟
//}TrPLinux联盟
}TrPLinux联盟
setTimeout("resize()",1000);TrPLinux联盟
}TrPLinux联盟
resize();
然后就在网页上调用resize();一旦窗口大小有变化,或是分辩率有变化,都能确保能够实现真正意义上的自适应.
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论