javascript 獲取窗口大小

  1. var x = document.body.scrollLeft; 
  2. var y = document.body.scrollTop;
  3. //獲取屏幕寬度
  4.    availWidth = parseInt(window.screen.availWidth);
  5.    availHeight = parseInt(window.screen.availHeight);
  6. //獲取可見區域 寬度 高度
  7.    availWidth = parseInt(document.body.clientWidth);
  8.    availHeight = parseInt(document.body.clientHeight);
  9.         
  10. 可見區域高度:document.body.clientHeight
  11. 總高度:document.body.scrollHeight
  12. 可見區域寬度:document.body.clientWidth
  13. 總寬度:document.body.scrollWidth
  14. ==============================================================
  15. var getWindowInfo=function()
  16. {
  17. var scrollX=0,scrollY=0,width=0,height=0,contentWidth=0,contentHeight=0;
  18. if(typeof(window.pageXOffset)=='number')
  19. {
  20.  scrollX=window.pageXOffset;
  21.  scrollY=window.pageYOffset;
  22. }
  23. else if(document.body&&(document.body.scrollLeft||document.body.scrollTop))
  24. {
  25.  scrollX=document.body.scrollLeft;
  26.  scrollY=document.body.scrollTop;
  27. }
  28. else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop))
  29. {
  30.  scrollX=document.documentElement.scrollLeft;
  31.  scrollY=document.documentElement.scrollTop;
  32. }
  33. if(typeof(window.innerWidth)=='number')
  34. {
  35.  width=window.innerWidth;
  36.  height=window.innerHeight;
  37. }
  38. else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight))
  39. {
  40.  width=document.documentElement.clientWidth;
  41.  height=document.documentElement.clientHeight;
  42. }
  43. else if(document.body&&(document.body.clientWidth||document.body.clientHeight))
  44. {
  45.  width=document.body.clientWidth;
  46.  height=document.body.clientHeight;
  47. }
  48. if(document.documentElement&&(document.documentElement.scrollHeight||document.documentElement.offsetHeight))
  49. {
  50.  if(document.documentElement.scrollHeight>document.documentElement.offsetHeight){
  51.   contentWidth=document.documentElement.scrollWidth;
  52.   contentHeight=document.documentElement.scrollHeight;
  53.  }
  54.  else
  55.  {
  56.   contentWidth=document.documentElement.offsetWidth;
  57.   contentHeight=document.documentElement.offsetHeight;
  58.  }
  59. }
  60. else if(document.body&&(document.body.scrollHeight||document.body.offsetHeight))
  61. {
  62.  if(document.body.scrollHeight>document.body.offsetHeight)
  63.  {
  64.   contentWidth=document.body.scrollWidth;
  65.   contentHeight=document.body.scrollHeight;
  66.  }else{
  67.   contentWidth=document.body.offsetWidth;
  68.   contentHeight=document.body.offsetHeight;
  69.  }
  70. }
  71. else
  72. {
  73.  contentWidth=width;
  74.  contentHeight=height;
  75. }
  76.  if(height>contentHeight)
  77.  height=contentHeight;
  78.  if(width>contentWidth)
  79.  width=contentWidth;
  80.  var rect=new Object();
  81.  rect.ScrollX=scrollX;
  82.  rect.ScrollY=scrollY;
  83.  rect.Width=width;
  84.  rect.Height=height;
  85.  rect.ContentWidth=contentWidth;
  86.  rect.ContentHeight=contentHeight;
  87.  return rect;
  88. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章