2010-09-20 137 views
3

我試圖弄清楚這是行不通的。 它曾經照顧的高度,而不是寬度,但我根本無法得到它的工作。 任何人都可以指出我的問題的正確方向嗎?Javascript windowwidth - 調整大小

  function getWindowWidth() { 
     var windowWidth = 0; 
     if (typeof(window.innerWidth) == 'number') { 
      innerWidth = window.innerWidth; 
     } 
     else { 
      if (document.documentElement && document.documentElement.clientWidth) { 
       windowWidth = document.documentElement.clientWidth; 
      } 
      else { 
       if (document.body && document.body.clientWidth) { 
        windowWidth = document.body.clientWidth; 
       } 
      } 
     } 
     return windowWidth; 
    } 
    function Removewhensmall(id) { 
     if (document.getElementById) { 
      var windowWidth = getWindowWidth(); 
      if (windowWidth > 0) { 


       var contentElement = document.getElementById(id); 
       var contentWidth = contentElement.offsetWidth; 

       if (windowWidth < 600) { 
        contentElement.style.display = 'none'; 


       } 


      } 
     } 
    } 
    window.onload = function() { 

Removewhensmall('rightwrap'); 
    Removewhensmall('leftwrap2'); 

    } 
    window.onresize = function() { 

      Removewhensmall('rightwrap'); 
    Removewhensmall('leftwrap2'); 

    } 

回答

3
if (typeof(window.innerWidth) == 'number') { 
     innerWidth = window.innerWidth; 
    } 

不應該說是

if (typeof(window.innerWidth) == 'number') { 
     windowWidth = window.innerWidth; 
    } 

並進一步在代碼

var contentWidth = contentElement.offsetWidth; 

定義,但contentWidth永遠不會再使用......

此外,你應該利用elseif()預防許多嵌套的if-clausules。

+0

美麗! 「var contentwidth」是遠處過去的剩餘字符。 – Troels 2010-09-20 13:53:20

+0

if(typeof(window.innerWidth)=='number'){ windowWidth = window.innerWidth; } 是主要問題。 – Troels 2010-09-20 13:54:06