码迷,mamicode.com
首页 > 移动开发 > 详细

移动端自动生成 meta js

时间:2020-01-17 20:58:57      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:result   doc   idt   let   device   initial   span   ios   tee   

(function () {
    const ua = navigator.userAgent;
    const android = ua.match(/Android \d\.\d/);
    // const ios = ua.match(/iPhone OS \d.\d.\d/);
    const androidVersion = android && android[0].match(/\d.\d/) && parseFloat(android && android[0].match(/\d.\d/)[0]);
    const baseWidth = 375;
    let nowWidth = baseWidth;
    // window.screen.width > 600 ? window.screen.width / 3 : window.screen.width;
    let basicFontSize = 32;

    if (nowWidth < 375) {
        nowWidth = 375;
    }
    basicFontSize *= (nowWidth / baseWidth);

    function rootFontSize(size) {
        const css = `html,body{font-size:${size}px !important;}`;
        const head = document.head || document.getElementsByTagName(‘head‘)[0];
        const style = document.createElement(‘style‘);

        style.type = ‘text/css‘;
        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }

        head.appendChild(style);
        // document.write(‘<style>html,body{font-size:‘ + size + ‘px !important;}</style>‘)
    }

    function _scale(zoom) {
        const meta = document.createElement(‘meta‘);
        meta.name = ‘viewport‘;
        meta.content = `width=device-width, initial-scale=${zoom}, minimum-scale=${zoom}, maximum-scale=${zoom}, user-scalable=no`;
        document.getElementsByTagName(‘head‘)[0].appendChild(meta);
    }

    function isScale() {
        let result = true;
        if (/KTU84P/.test(ua)) {
            result = false;
        }
        if (android) {
            if (androidVersion <= 4.3) {
                result = false;
            } else if (androidVersion <= 4.4) {
                const type = [/A31/, /R8207/, /R8170/, /R8270/, /R7c/];
                for (let i = 0; i < type.length; i += 1) {
                    if (type[i].test(ua)) {
                        result = false;
                    }
                }
            }
        }
        return result;
    }


    if (!isScale()) {
        rootFontSize(16);
        _scale(1);
    } else {
        _scale(0.5);
        rootFontSize(basicFontSize);
    }
}());

 

移动端自动生成 meta js

标签:result   doc   idt   let   device   initial   span   ios   tee   

原文地址:https://www.cnblogs.com/sybboy/p/12207242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!