zoukankan      html  css  js  c++  java
  • 移动端自动生成 meta js

    (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);
        }
    }());
  • 相关阅读:
    Could't creat any visual c# project in visual studio 2008 beta 2
    最近有点时间赶紧学习,好久没有写东西了
    三亚 渡假 不多, 以后要多去去 芒果很好吃
    windows8. 微软要颠覆PC传统模式
    IM
    windows8 预览版本 安装 VC6 + sp6
    windows 8 截图部分
    分享一个常识 如果你也和VC 打交道
    有时间看看, 热爱技术,不沉迷于技术
    LIVE555 Media Server
  • 原文地址:https://www.cnblogs.com/sybboy/p/12207242.html
Copyright © 2011-2022 走看看