2013-05-30 161 views
1

如何根据浏览器屏幕大小加载文件?这是我使用的代码:JS对于屏幕宽度

<script> 
if(iPhone) { <- screen size instead of device 
    document.write("<meta name="viewport" content=\"width=480\""+"/>"); 
} else { 
    document.write("<meta name="viewport" content=\"width=1024\""+"/>"); 
} 
</script> 

感谢

+0

你可以从这里开始https://developer.mozilla.org/en-US/docs/Web/API /窗口 – elclanrs

回答

0

你可以尝试:

<script> 
    if (window.screen.width < 481) { 
     document.write("<meta name="viewport" content=\"width=480\""+"/>"); 
    } else { 
     document.write("<meta name="viewport" content=\"width=1024\""+"/>"); 
    } 
</script> 

屏幕大小的代码很简单,因为它仅仅是window.screen.widthwindow.screen.height。除非您计划使用与该网站相同的设计模式设计网站,否则我不建议将网站定位到特定设备。

+0

谢谢。你如何结合两个项目,如最小宽度800和最大宽度1024? < 800 && > 1024? –

+0

@GregorySchultz是的,它会是:'window.screen.width <481 && window.screen.width> 1024'。 – Cary

+0

@GregorySchultz如果我的回答满足您的问题,那么您应该点击复选标记,以便您的问题可以关闭。 – Cary

0

您可以使用document.body.offsetWidth编写自己的Javascript。

例如如果(document.body.offsetWidth < 1024){...}

或使用就像一个图书馆:http://responsejs.com/