2013-08-04 24 views
1

如果這不行,我是一個真正的初學者,當談到JavaScript,所以我不知道爲什麼這是行不通的。我使用JavaScript而不是CSS媒體查詢的原因是因爲圖像正在使用JavaScript來打開和關閉,並且一旦它被調用,css似乎不會超過js。試圖顯示:無或顯示:內聯取決於與javasrcript屏幕的寬度

<script type="text/javascript"> 
    if (window.innerWidth > 1440) { 
     ('#art1').style.display = 'inline'; 
     ('#art1_lores').style.display = 'none'; 
    } 
    else { 
     ('#art1').style.display = 'none'; 
     ('#art1_lores').style.display = 'inline'; 
    } 

這是我在哪裏: http://www.dillonbrannick.com/redesign

所以右邊的圖標影響圖像左側,任何被點擊的CSS媒體查詢工作之前,但一旦你點擊其中一個前兩個圖標他們沒有,所以我想出了一個JavaScript解決方案可能是要走的路,但不幸的是我的嘗試不工作。

回答

1

你應該換你的代碼到window.onresize處理。例如像這樣:

var art1 = document.getElementById('art1'), 
    art1Lores = document.getElementById('art1_lores'); 

window.onresize = function() { 
    if (window.innerWidth > 440) { 
     art1.style.display = 'inline'; 
     art1Lores.style.display = 'none'; 
    } else { 
     art1.style.display = 'none'; 
     art1Lores.style.display = 'inline'; 
    } 
} 

window.onresize(); 

此外請確保您緩存DOM查詢,重新選擇節點一次又一次地降低性能。沒有不適用window.onresize條款:

http://jsfiddle.net/9J3cg/

+0

難道我說,如果這兩個「ART1」和「art1_lores」是顯示一個條款補充的嗎? – dillonbrannick

+1

不知道我明白了嗎? 'if(art1.style.display =='none'&& art1.style.display =='none')return;如果(window.innerWidth> 440){...'。 – dfsq

+0

我做了'window.onresize = function(){if(art1.style.display ==='inline'|| art1Lores.style.display ==='inline'){**您的代碼在上面**} else { art1.style.display ='none'; art1Lores.style.display ='none'; }' – dillonbrannick

0

你用jQuery嗎?你鏈接到你的jQuery文件? 如果沒有,你應該寫:

document.getElementById('art1').style.display = 'none'; 

或也許嘗試

document.getElementById("art1").style.display = 'none'; 
0

試試這個:

var element = document.getElementById('art1'); 

if (window.innerWidth > 1440) { 
    element.style.display = 'inline'; 
    element.style.display = 'none'; 
} 
else { 
    element.style.display = 'none'; 
    element.style.display = 'inline'; 
} 
相關問題