移动端自动生成 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);
    }
}());
复制代码

 

posted @   创业男生  阅读(424)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2013-01-17 DataTable批量插入数据库四种方式比较
点击右上角即可分享
微信分享提示