• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
可樂_Thompson
博客园    首页    新随笔    联系   管理    订阅  订阅
Web移动端项目适配(适用于原生JavaScript,Vue,React等项目中)

下面是移动端项目适配解决方案,可以将它直接写在index.html文件中,也可以将它写在main.js中或者写在通过import引入的文件中:

(function(win) {
    var doc = win.document;
    var docEl = doc.documentElement;
    var tid;
    function refreshRem() {
        var width = docEl.getBoundingClientRect().width;
        //比例按750设计稿 除以10 则单位 px/75 为最终结果。
        //比例按750设计稿 除以7.5 则单位 px/100 为最终结果。
        var rem = width / 7.5; 
        docEl.style.fontSize = rem + 'px';
    }
    win.addEventListener('resize', function() {
        clearTimeout(tid);
        tid = setTimeout(refreshRem, 300);
    }, false);
    win.addEventListener('pageshow', function(e) {
         if (e.persisted) {
             clearTimeout(tid);
             tid = setTimeout(refreshRem, 300);
        }
    }, false);
    refreshRem();
})(window); 

 

posted on 2020-05-15 11:32  可樂_Thompson  阅读(239)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3