美文网首页
VUE设置px和rem的转换

VUE设置px和rem的转换

作者: KC莲 | 来源:发表于2020-03-18 18:57 被阅读0次

    安装插件

    npm install postcss-pxtorem -S
    

    配置插件

    需要新建配置文件
    .postcssrc.js


    image.png

    文件内容如下

    module.exports = {
      "plugins": {
        // to edit target browsers: use "browserlist" field in package.json
        "postcss-pxtorem": {
          rootValue: 100,
          propList: ['*'],
          minPixelValue: 1
        }
      }
    }
    
    

    在index.html中引入配置文件

    web配置

    <script>
         (function (a, b) {
           // 页面自适应脚本
           var c = a.documentElement,
               d = "orientationchange" in window ? "orientationchange" : "resize",
               e = window.recalc = function () {
                 var a = c.clientWidth;
                 if (a) {
                   var fontsize = Math.floor(100 * (a / 1920));
                   if (fontsize < 50) {
                     fontsize = 50;
                   }
                   if (fontsize >= 100) {
                     fontsize = 100;
                   }
                   c.style.fontSize = fontsize + "px"
                 }
               };
           a.addEventListener && (b.addEventListener(d, e, !1), a.addEventListener("DOMContentLoaded", e, !1));
         })(document, window);
       </script>
    

    移动端配置

    <script>
        !function (window) {
    
          /* 设计图文档宽度 */
          var docWidth = 750;
    
          var doc = window.document,
            docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
    
          var recalc = (function refreshRem() {
            var clientWidth = docEl.getBoundingClientRect().width;
    
            /* 8.55:小于320px不再缩小,11.2:大于420px不再放大 */
            docEl.style.fontSize = Math.max(Math.min(20 * (clientWidth / docWidth), 11.2), 8.55) * 5 + 'px';
    
            return refreshRem;
          })();
    
          /* 添加倍屏标识,安卓为1 */
          docEl.setAttribute('data-dpr', window.navigator.appVersion.match(/iphone/gi) ? window.devicePixelRatio : 1);
    
          if (/iP(hone|od|ad)/.test(window.navigator.userAgent)) {
            /* 添加IOS标识 */
            doc.documentElement.classList.add('ios');
            /* IOS8以上给html添加hairline样式,以便特殊处理 */
            if (parseInt(window.navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/)[1], 10) >= 8)
              doc.documentElement.classList.add('hairline');
          }
    
          if (!doc.addEventListener) return;
          window.addEventListener(resizeEvt, recalc, false);
          doc.addEventListener('DOMContentLoaded', recalc, false);
    
        }(window);
    
      </script>
    

    相关文章

      网友评论

          本文标题:VUE设置px和rem的转换

          本文链接:https://www.haomeiwen.com/subject/nkbcyhtx.html