以跨浏览器的方式查找视口的确切高度和宽度(无Prototype / jQuery)

我正在尝试查找浏览器视口的确切高度和宽度,但是我怀疑Mozilla或IE给我的号码不正确。这是我的身高方法:


var viewportHeight = window.innerHeight || 

                     document.documentElement.clientHeight || 

                     document.body.clientHeight;

我还没有开始做宽度,但是我猜它将会是类似的东西。


有没有更正确的方式来获取此信息?理想情况下,我希望该解决方案也能与Safari / Chrome /其他浏览器一起使用。


斯蒂芬大帝
浏览 387回答 3
3回答

慕哥9229398

您可以使用较短的版本:<script type="text/javascript"><!--function getViewportSize(){&nbsp; &nbsp; var e = window;&nbsp; &nbsp; var a = 'inner';&nbsp; &nbsp; if (!('innerWidth' in window)){&nbsp; &nbsp; &nbsp; &nbsp; a = 'client';&nbsp; &nbsp; &nbsp; &nbsp; e = document.documentElement || document.body;&nbsp; &nbsp; }&nbsp; &nbsp; return { width : e[ a+'Width' ] , height : e[ a+'Height' ] }}//--></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript