Last active
December 16, 2015 06:49
-
-
Save oklai/5394379 to your computer and use it in GitHub Desktop.
获取当前viewport的宽度
This file contains hidden or 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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Get Viewport Width</title> | |
</head> | |
<body> | |
<div id="log"></div> | |
<script type="text/javascript"> | |
function getViewportWidth () { | |
var viewportwidth; | |
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight | |
if (typeof window.innerWidth != 'undefined') { | |
viewportwidth = window.innerWidth; | |
} | |
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document) | |
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) { | |
viewportwidth = document.documentElement.clientWidth; | |
} | |
// older versions of IE | |
else { | |
viewportwidth = document.body.clientWidth; | |
} | |
return viewportwidth; | |
} | |
var logElem = document.getElementById('log'); | |
logElem.innerHTML = getViewportWidth(); | |
window.onresize = function () { | |
logElem.innerHTML = getViewportWidth(); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment