Vue项目配置 rem适配

1.编写rem 适配文件 remConfig.js

export default function() {
  // var devicePixelRatio = 1;
  // var scale = 1 / devicePixelRatio;
  // document.querySelector('meta[name="viewport"]').setAttribute('content','initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no');
  // 7.5根据设计稿的横向分辨率/100得来
  // alert(document.documentElement.clientWidth)
  var deviceWidth = document.documentElement.clientWidth;
  // var deviceWidth = window.screen.availWidth
  // alert(navigator.userAgent)
  // alert(deviceWidth)
  // console.log(navigator.userAgent)
  if(deviceWidth > 750) {
    //deviceWidth = 7.5 * 50;
  }
 
  //document.documentElement.style.fontSize = deviceWidth / 7.5 + 'px';
  document.documentElement.style.fontSize = 40 * (deviceWidth / 1366) + 'px';
  // 禁止双击放大
  document.documentElement.addEventListener('touchstart', function (event) {
    if (event.touches.length > 1) {
      event.preventDefault();
    }
  }, false);
  var lastTouchEnd = 0;
  document.documentElement.addEventListener('touchend', function (event) {
    var now = Date.now();
    if (now - lastTouchEnd <= 300) {
      event.preventDefault();
    }
    lastTouchEnd = now;
  }, false);
}

2.修改 main.js文件,加入以下两行代码

import remConfig from './config/remConfig'

remConfig();

3.使用

现在可以在项目中使用 rem了,例如:  font-size: 0.5rem;

发布了95 篇原创文章 · 获赞 216 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/xukongjing1/article/details/101348942
今日推荐