简体   繁体   English

屏幕宽度的JS

[英]JS for screen width

How do you load a file based on the browser screen size? 如何根据浏览器屏幕大小加载文件? This is the code I'm using: 这是我正在使用的代码:

<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>

Thanks 谢谢

You could try: 您可以尝试:

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

The code for screen size is simple as it is just window.screen.width and window.screen.height . 屏幕尺寸的代码很简单,因为它只是window.screen.widthwindow.screen.height I would not recommend targeting a website towards a specific device unless you plan to design the website with the same design patterns as that website. 除非您打算使用与该网站相同的设计模式来设计网站,否则我不建议将网站定位为使用特定设备。

You could write your own Javascript using document.body.offsetWidth. 您可以使用document.body.offsetWidth编写自己的Javascript。

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

Or use a library like: http://responsejs.com/ 或使用类似以下的库: http : //responsejs.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM