简体   繁体   English

在Javascript中(或通过CSS)检测小(移动)屏幕

[英]Detect small (mobile) screen in Javascript (or via css)

I want to make some of the fonts on my website larger, if a visitor is using a small screen. 如果访问者使用的是小屏幕,我希望将网站上的某些字体放大。 Ideally without jquery, as I want to do this early on in the page load, and I don't want to load jquery until later, for faster loading. 理想情况下不使用jquery,因为我想在页面加载的早期进行此操作,并且我不想在以后加载jquery,以便更快地进行加载。

The best I have come up with, is to check for screen size. 我想出的最好办法是检查屏幕尺寸。 But this is far from perfect. 但这远非完美。 An iphone4 has relatively large size, but small screen, while some old netbook might have a smaller resolution but a larger screen. iphone4的尺寸相对较大,但屏幕较小,而某些旧上网本的分辨率可能较小,但屏幕较大。 I guess what I really want is some variant of screen "DPI". 我想我真正想要的是屏幕“ DPI”的某些变体。

If there is some css way of saying "on a small screen do this, else on a large screen do that" that would work too. 如果有某种css方式说“在小屏幕上执行此操作,否则在大屏幕上执行该操作”也可以。

In CSS2 there's a media property and in CSS3 this can be used to do media queries . 在CSS2中有一个media属性,在CSS3中可用于进行媒体查询 It's not supported on all browsers, but it may be okay to use since your small devices like iPhone etc do support it. 并非所有浏览器都支持该功能,但是可以使用,因为您的小型设备(如iPhone等)确实支持它。

 @media screen and (min-width: 781px) and (max-width: 840px) {
    body {
      font-size: 13px;
    }
  }

This site doesn't care about IE, try it in FF or Safari, change the browser width and notice how the width changes using this property. 该站点不关心IE,可以在FF或Safari中尝试它,更改浏览器的宽度,并注意使用此属性来更改宽度。

Media Queries are the key and are a lot of fun to use. 媒体查询是关键,并且使用起来很有趣。

See http://jsfiddle.net/neebz/kn7y3/ and change the width/height of the 'Result' panel to see it working. 请参阅http://jsfiddle.net/neebz/kn7y3/并更改“结果”面板的宽度/高度,以查看其工作情况。

Example taken from : http://www.maxdesign.com.au/articles/css3-media-queries/media-sample/ 示例取自: http : //www.maxdesign.com.au/articles/css3-media-queries/media-sample/

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

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