April 5th, 2009

Javascript 获取窗口宽度和高度值

JavaScript, by chris.

获取页面高度,窗口高度,滚动条高度等参数值:

  1. function getPageScroll(){
  2. var yScroll;
  3. if (self.pageYOffset) {
  4. yScroll = self.pageYOffset;
  5. } else if (document.documentElement && document.documentElement.scrollTop){   // Explorer 6 Strict
  6. yScroll = document.documentElement.scrollTop;
  7. } else if (document.body) {// all other Explorers
  8. yScroll = document.body.scrollTop;
  9. }
  10. arrayPageScroll = new Array(”,yScroll)
  11. return arrayPageScroll;
  12. }
  13. function getPageSize(){
  14. var xScroll, yScroll;
  15. if (window.innerHeight && window.scrollMaxY) {
  16. xScroll = document.body.scrollWidth;
  17. yScroll = window.innerHeight + window.scrollMaxY;
  18. } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  19. xScroll = document.body.scrollWidth;
  20. yScroll = document.body.scrollHeight;
  21. } else { // Explorer Mac…would also work in Explorer 6 Strict, Mozilla and Safari
  22. xScroll = document.body.offsetWidth;
  23. yScroll = document.body.offsetHeight;
  24. }
  25. var windowWidth, windowHeight;
  26. if (self.innerHeight) {  // all except Explorer
  27. windowWidth = self.innerWidth;
  28. windowHeight = self.innerHeight;
  29. } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  30. windowWidth = document.documentElement.clientWidth;
  31. windowHeight = document.documentElement.clientHeight;
  32. } else if (document.body) { // other Explorers
  33. windowWidth = document.body.clientWidth;
  34. windowHeight = document.body.clientHeight;
  35. }
  36. // for small pages with total height less then height of the viewport
  37. if(yScroll < windowHeight){
  38. pageHeight = windowHeight;
  39. } else {
  40. pageHeight = yScroll;
  41. }
  42. if(xScroll < windowWidth){
  43. pageWidth = windowWidth;
  44. } else {
  45. pageWidth = xScroll;
  46. }
  47. arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
  48. return arrayPageSize;
  49. }
收藏与分享

Back Top

Responses to “Javascript 获取窗口宽度和高度值”

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Back Top