Created
June 12, 2012 01:40
-
-
Save to/2913882 to your computer and use it in GitHub Desktop.
Window Size, Media Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0"> | |
</head> | |
<body> | |
<pre id="out"></pre> | |
</body> | |
<script> | |
function checkSizes(){ | |
var out = [ | |
'window.devicePixelRatio: ' + window.devicePixelRatio, | |
'window.innerWidth: ' + window.innerWidth, | |
'window.innerHeight: ' + window.innerHeight, | |
'window.screen.width: ' + window.screen.width, | |
'window.screen.height: ' + window.screen.height, | |
'', | |
]; | |
var tests = { | |
'-webkit-device-pixel-ratio' : window.devicePixelRatio, | |
'-moz-device-pixel-ratio' : window.devicePixelRatio, | |
'device-pixel-ratio' : window.devicePixelRatio, | |
'device-aspect-ratio' : window.screen.width + '/' + window.screen.height, | |
'aspect-ratio' : window.innerWidth + '/' + window.innerHeight, | |
'width' : window.innerWidth + 'px', | |
'device-width' : window.screen.width + 'px', | |
'height' : window.innerHeight + 'px', | |
'device-height' : window.screen.height + 'px', | |
} | |
for(var prop in tests){ | |
var test = prop + ': ' + tests[prop]; | |
if(window.matchMedia('(' + test + ')').matches) | |
out.push(test); | |
} | |
document.getElementById('out').textContent = out.join('\n'); | |
} | |
checkSizes(); | |
window.addEventListener('resize', checkSizes, true); | |
</script> | |
</html> |
device-widthは機器の横幅。iPadでは、縦に持っても横に持っても短辺の768pxのまま変わらない。このため横向きに持ち、viewportでwidth=device-widthを指定すると予期しない値になる(拡大される)。Androidは持つ向きに応じてdevice-widthも変化した(Chrome/Androidブラウザ共に)。
Android Chromeのdevice-heightはステータスバーの領域を除算した値になる。Android ブラウザのdevice-heightは、デバイス本来の高さになる。window.screen.heightも同一の動作。
デバイスピクセル比が2のデバイスで、viewportのスケールを0.5にし、ピクセル比を1に調整しても、window.devicePixelRatioの値は変わらない。現在の値ではなく、本来の持っている能力の値のよう。
resolutionのクエリ(dip)は、どこでもマッチしなかった。viewportにtarget-densitydpi=device-dpiを指定するとエラーになる。
Retinaの新iPadも昔のiPadはdevice-widthとdevice-heightが同じ。本当のデバイスピクセル数は、この値にデバイスピクセル比を乗算したものになる。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Firefox、Chrome、iOS Safari、Android ブラウザー、Android Chromeで全てのメディアクエリがマッチした。