视口-viewport

视口HTML标签:

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

添加不可缩放属性:

    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1, minimum-scale=1">

JavaScript中获取视口参数:

    <script>
        console.log(window.innerWidth);
        console.log(document.documentElement.clientWidth);
        console.log(document.documentElement.getBoundingClientRect().width);
    </script>

若要考虑到兼容性问题,建议这么写:

    <script>
        var viewWidth = document.documentElement.clientWidth || window.innerWidth;
    </script>

注意,一般不建议以下写法,因为有时候可能检测到是的是分辨率(物理像素):

console.log(screen.witdth);

获取视口像素比DPR(物理像素:CSS像素):

console.log(window.devicePixelRatio);
上一篇:微信小程序页面适配之viewport


下一篇:移动端单位相关概念