获取浏览器视口大小

原文链接: https://www.mk2048.com/blog/blog.php?id=h02ja10cj0ab&title=%E8%8E%B7%E5%8F%96%E6%B5%8F%E8%A7%88%E5%99%A8%E8%A7%86%E5%8F%A3%E5%A4%A7%E5%B0%8F
 1 window.onload = function() {
 2     alert("width: "   getPageScale().pageWidth   ", height: "   getPageScale().pageHeight);
 3 };
 4 
 5 /**
 6  * 获取浏览器视口大小
 7  * @return {object} 宽,高
 8  */
 9 function getPageScale(){
10     var pageWidth = window.innerWidth,
11         pageHeight = window.innerHeight;
12 
13     if (typeof pageWidth !== "number") {
14         if (document.compatMode === "CSS1Compat") {
15             pageWidth = document.documentElement.clientWidth;
16             pageHeight = document.documentElement.clientHeight;
17         }else{
18             pageWidth = document.body.clientWidth;
19             pageHeight = document.body.clientHeight;
20         }
21     }
22 
23     return {pageWidth, pageHeight};
24 }

更多专业前端知识,请上 【猿2048】www.mk2048.com

猜你喜欢

转载自blog.csdn.net/qq_29069777/article/details/102758980