html5移动端适配;检测浏览器信息函数

html5移动端适配

js 复制代码
//动态改变font-size大小
(function changeFontSize() {
    let resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'
    if (!isPC()) {
        let docEl = document.documentElement;
            // recalc = function () {
                let clientWidth = docEl.clientWidth;
                docEl.style.fontSize = 100 * (clientWidth / 375)  + 'px';
                let scaledFontSize = parseInt(window.getComputedStyle(docEl, null).getPropertyValue('font-size'));
                let scaleFactor = 100 * (clientWidth / 375) / scaledFontSize;
                let originRootFontSize = parseInt(window.getComputedStyle(document.documentElement, null).getPropertyValue('font-size'));
                docEl.style.fontSize = originRootFontSize * scaleFactor * scaleFactor + 'px';
            // };
    } else {
        let docEl = document.documentElement;
        docEl.style.fontSize = 'unset'
    }
    // if (!doc.addEventListener) return;
    window.addEventListener(resizeEvt, changeFontSize, false);
    document.addEventListener('DOMContentLoaded', changeFontSize, false);
})(document, window);

function isPC() {
    let userAgentInfo = navigator.userAgent;
    let Agents = ["Android", "iPhone",
        "SymbianOS", "Windows Phone",
        "iPad", "iPod"];
    let isPc = true;
    for (let i = 0;i< Agents.length; i++) {
        if (userAgentInfo.indexOf(Agents[i]) > 0) {
            isPc = false;
            break;
        }
    }
    if (document.documentElement.clientWidth <= 640) {
        isPc = false;
    }
    return isPc;
}

浏览器信息检测

js 复制代码
//判断浏览器信息
function getNavigationInfo () {
    const ua = navigator.userAgent
    let browserInfo = {
        trident: ua.indexOf('Trident') > -1, // IE浏览器 trident内核
        presto: ua.indexOf('Presto') > -1, // opera浏览器 presto内核
        webKit: ua.indexOf('AppleWebKit') > -1, // chrome safari浏览器 webkit内核
        gecko: ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') == -1, //firefox浏览器 gecko内核
        mobile: !!ua.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端
        ios: !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
        android: ua.indexOf('Android') > -1 || ua.indexOf('Linux') > -1, // android终端或UC浏览器
        iPad: ua.indexOf('iPad') > -1, //iPad终端
        webApp: ua.indexOf('Safari') == -1, //是否web应用程序,没有头部与底部
        openOnVchat: ua.toLowerCase().match(/MicroMessenger/i) == "MicroMessenger".toLowerCase(), // 在微信中打开
        openOnWeiBo: ua.toLowerCase().match(/WeiBo/i) == "Weibo".toLowerCase(), // 在新浪微博客户端打开
        openOnQQ: ua.toLowerCase().match(/QQ/i) == "QQ".toLowerCase(),// 在QQ端打开
    }
    return browserInfo;
}

文本可编辑

在文本标签上加上属性contenteditable="true"

深拷贝对象

js 复制代码
function deepClone(obj) {
  if (obj === null || typeof obj !== 'object') {
    return obj;
  }

  let clone = Array.isArray(obj) ? [] : {};

  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      clone[key] = deepClone(obj[key]);
    }
  }

  return clone;
}
相关推荐
接着奏乐接着舞。1 小时前
【2026年7月最新】69道RAG面试题
前端·人工智能·后端·aigc·embedding·rag
以和为贵1 小时前
前端也能搞懂 Agent:从 Function Calling 到自主编排
前端·人工智能·架构
风止何安啊1 小时前
🚦 前端并发请求 “交通管制”:别让你的接口堵成早高峰
前端·javascript·面试
J船长1 小时前
扫盲烂笔头:A 记录、CNAME 的作用, Nginx的用途,为啥叫反向代理
前端
妙码生花1 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(二十六):icon 组件封装
前端·vue.js·typescript
李明卫杭州1 小时前
Vue2 & Vue3 中 Router `props` 配置详解:彻底摆脱 `$route` 依赖
前端·vue.js·前端框架
掘金一周1 小时前
裁员了,公司很爽快,赔偿一分没少 | 沸点周刊 7.8
前端·人工智能·后端
熊猫钓鱼>_>1 小时前
2026 前端的三场底层革命:当 Vue、React Native 和 Three.js 同时去掉中间层
前端·javascript·vue.js·react native·架构·webgl·three.js
名字还没想好☜2 小时前
React useEffect 依赖数组踩坑:闭包陷阱与清理函数
前端·javascript·react.js·react·useeffect
宸翰2 小时前
uni-app 设置 Android 底部虚拟导航栏背景色
android·前端·uni-app