准确获得页面、窗口高度及宽度的JS

时间:2021-05-25

functiongetPageSize(){

varxScroll,yScroll;

if(window.innerHeight&&window.scrollMaxY){
xScroll=document.body.scrollWidth;
yScroll=window.innerHeight+window.scrollMaxY;
}elseif(document.body.scrollHeight>document.body.offsetHeight){//allbutExplorerMac
xScroll=document.body.scrollWidth;
yScroll=document.body.scrollHeight;
}else{//ExplorerMac...wouldalsoworkinExplorer6Strict,MozillaandSafari
xScroll=document.body.offsetWidth;
yScroll=document.body.offsetHeight;
}

varwindowWidth,windowHeight;
if(self.innerHeight){//allexceptExplorer
windowWidth=self.innerWidth;
windowHeight=self.innerHeight;
}elseif(document.documentElement&&document.documentElement.clientHeight){//Explorer6StrictMode
windowWidth=document.documentElement.clientWidth;
windowHeight=document.documentElement.clientHeight;
}elseif(document.body){//otherExplorers
windowWidth=document.body.clientWidth;
windowHeight=document.body.clientHeight;
}

//forsmallpageswithtotalheightlessthenheightoftheviewport
if(yScroll<windowHeight){
pageHeight=windowHeight;
}else{
pageHeight=yScroll;
}

//forsmallpageswithtotalwidthlessthenwidthoftheviewport
if(xScroll<windowWidth){
pageWidth=windowWidth;
}else{
pageWidth=xScroll;
}


arrayPageSize=newArray(pageWidth,pageHeight,windowWidth,windowHeight)
returnarrayPageSize;
}
有幸找到了你的这个东东,帮我解决了问题,不过仔细看了下,好象大大的有点问题,参数值和名称上看好象有点对不上号哦.
//forsmallpageswithtotalheightlessthenheightoftheviewport
if(yScroll<windowHeight){
pageHeight=yScroll;
}else{
pageHeight=windowHeight;
}

//forsmallpageswithtotalwidthlessthenwidthoftheviewport
if(xScroll<windowWidth){
pageWidth=xScroll;
}else{
pageWidth=windowWidth;
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章