您可以尝试以下方法:
function getViewport() {
var viewPortWidth;
var viewPortHeight;
if (typeof window.innerWidth != 'undefined') {
viewPortWidth = window.innerWidth,
viewPortHeight = window.innerHeight
}
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0) {
viewPortWidth = document.documentElement.clientWidth,
viewPortHeight = document.documentElement.clientHeight
}
else {
viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
viewPortHeight = document.getElementsByTagName('body')[0].clientHeight
}
return [viewPortWidth, viewPortHeight];
}
(http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/)
但是,甚至不可能在所有浏览器中都获得视口信息(例如,古怪模式下的IE6)。但是上面的脚本应该做得很好:-)