Is there a way to get the users device width, as opposed to viewport width, using javascript?
CSS media queries offer this, as I can say
@media screen and (max-width:640px) {
/* ... */
}
and
@media screen and (max-device-width:960px) {
/* ... */
}
This is useful if I'm targeting smartphones in landscape orientation. For example, on iOS a declaration of max-width:640px
will target both landscape and portrait modes, even on an iPhone 4. This is not the case for Android, as far as I can tell, so using device-width in this instance successfully targets both orientations, without targeting desktop devices.
However, if I'm invoking a javascript binding based on device width, I appear to be limited to testing the viewport width, which means an extra test as in the following,
if ($(window).width() <= 960 && $(window).height <= 640) { /* ... */ }
This doesn't seem elegant to me, given the hint that device width is available to css.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…