2013-01-16 45 views
0

我想用jQuery進行媒體查詢,但無法正常工作。爲什麼不在jQuery中使用媒體查詢?

if ((screen.width>=20) || (screen.width<300)){ 
     width="190"; //Adjust the width of the image 
     height="170"; //Adjust the height of the image 
    } 
    if ((screen.width>=300) || (screen.width<350)){ 
     width="240"; height="200"; 
    } 
    if ((screen.width>=351) || (screen.width<400)){ 
     width="280"; height="220"; 
    } 
    if ((screen.width>=401) || (screen.width<599)){ 
     width="320"; height="240"; 
    } 
    if ((screen.width>=600) || (screen.width<757)){ 
     width="440"; height="320"; 
    } 
    if ((screen.width>=758) || (screen.width<899)){ 
     width="320"; height="240"; 
    } 

由於添加了新內容而導致加載插件時調用該代碼。這是一個單頁面應用程序。內容加載時不刷新瀏覽器。 我需要使用jquery的媒體查詢,因爲widthheight是插件的變量。

有什麼想法?謝謝

+1

什麼是'screen'?它從哪裏來的?你是不是指'$(window).width()'和'$(window).height()'? –

+0

屏幕是什麼?屏幕是一個jQuery對象嗎?無論如何,插件是什麼? – Alexander

+1

如果您正在尋找jQuery/Javascript可以評估的媒體查詢,請使用Modernizr插件的mq()方法:http://modernizr.com/docs/#mq – saschoar

回答

1

這是你想要?

if ((window.innerWidth>=20) || (window.innerWidth<300)){ 
     width="190"; //Adjust the width of the image 
     height="170"; //Adjust the height of the image 
    } 
    if ((window.innerWidth>=300) || (window.innerWidth<350)){ 
     width="240"; height="200"; 
    } 
    if ((window.innerWidth>=351) || (window.innerWidth<400)){ 
     width="280"; height="220"; 
    } 
    if ((window.innerWidth>=401) || (window.innerWidth<599)){ 
     width="320"; height="240"; 
    } 
    if ((window.innerWidth>=600) || (window.innerWidth<757)){ 
     width="440"; height="320"; 
    } 
    if ((window.innerWidth>=758) || (window.innerWidth<899)){ 
     width="320"; height="240"; 
    }