js判断手机是苹果(IOS)还是安卓(android) H5手机端自适应宽高

<script>
        $(document).ready(function () {
            
            //var w = document.documentElement.clientWidth;
            //var h = document.documentElement.clientHeight;
            var w = window.screen.width;//取屏幕分辨率
            var h = window.screen.height;
            var u = navigator.userAgent;

            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
            
            if (isAndroid) {
                h = parseInt(h) - parseInt(150);
            }
            if (isiOS) {
                h = parseInt(h) - parseInt(parseInt(h)*0.2);
            }
            w = parseInt(w) - parseInt(20);

            //$("#div01").attr("style", "width:" + w + "px; height:" + h + "px; border:1px solid red;");
            //$("#div01").attr("style", "width:" + w + "px; height:100%; border:1px solid red;");
            $("#if01").attr("style", "width:" + w + "px; height:" + h + "px;  margin:0.1rem; border:1px solid #000;");

            //$("#if01").attr("width", w);
            //$("#if01").attr("height", h);

        });
    </script>
<body>
    
    <div id="div01" style="width:100%; height:100%; border:1px solid red;">
        
    <iframe id="if01" src="https://p.qiao.baidu.com/cps/chat?siteId=6959701&userId=7009868"   ></iframe>
    </div>
</body>

 

posted @ 2020-09-05 16:57  离。  阅读(61)  评论(0编辑  收藏