rem 屏幕移动端适配相关资料echart,解决datav移动端问题

datav的全屏布局经过测试无法适配,特别是横屏,所以就没用了。https://github.com/amfe/lib-flexible/blob/2.0/index.js
原理是当窗口变化,根html字体会变化,rem则是根据此变化而显示对应的大小。

(function flexible(window, document) {
  var docEl = document.documentElement;
  var dpr = window.devicePixelRatio || 1;

  var isAndroid = window.navigator.appVersion.match(/android/gi)
  var isIPhone = window.navigator.appVersion.match(/iphone/gi)
  var devicePixelRatio = window.devicePixelRatio
  if (isIPhone) {
    // iOS下,对于2和3的屏,用2倍的方案,其余的用1倍方案
    if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
      dpr = 3
    } else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)) {
      dpr = 2
    } else {
      dpr = 1
    }
  } else {
    // 其他设备下,仍旧使用1倍的方案
    dpr = 1
  }



  // adjust body font size
  function setBodyFontSize() {
    if (document.body) {
      document.body.style.fontSize = 12 * dpr + "px";
    } else {
      document.addEventListener("DOMContentLoaded", setBodyFontSize);
    }
  }
  setBodyFontSize();

  // set 1rem = viewWidth / 10
  function setRemUnit() {
    var rem = docEl.clientWidth / 150;
    docEl.style.fontSize = rem + "px";
  }


/*
  const screenWidth = 1800
  const scale = screenWidth / 16
  const htmlWidth = document.documentElement.clientWidth || document.body.clientWidth

  //   var rem = document.documentElement.clientWidth / 24;
  //   document.documentElement.style.fontSize = rem + "px";
  const htmlDom = document.getElementsByTagName('html')[0]
  // 设置根元素字体大小
  htmlDom.style.fontSize = htmlWidth / scale + 'px'*/

  setRemUnit();

  // reset rem unit on page resize
  window.addEventListener("resize", setRemUnit);
  window.addEventListener("pageshow", function(e) {
    if (e.persisted) {
      setRemUnit();
    }
  });

  // detect 0.5px supports
  if (dpr >= 2) {
    var fakeBody = document.createElement("body");
    var testElement = document.createElement("div");
    testElement.style.border = ".5px solid transparent";
    fakeBody.appendChild(testElement);
    docEl.appendChild(fakeBody);
    if (testElement.offsetHeight === 1) {
      docEl.classList.add("hairlines");
    }
    docEl.removeChild(fakeBody);
  }
})(window, document);

由于根字体变大了,body字体需要重置所以可以看到上面的body进行了调整。

echart
datav
不支持rem,所以直接import后调用此remToPx方法

export function remToPx(rem) {
    var fontSize = document.documentElement.style.fontSize;
    var result= Math.floor(rem * fontSize.replace('px', ''));
    return result
}

datav的那些边距无法调整,只能通过设置相对偏移进行微调
而装饰粗细,只能通过重写svg,自己实现,反向,则翻转样式

.flip-horizontal {
    transform: scaleX(-1);
}

针对pc和手机端进行不同的调整不同的rem单位

/* 小于750的属于iphone */

@media screen and (max-width: 750px) {
    #titlebarbg {
        height: 8rem !important;
        width: 35rem !important;;
    }
    #logostyle {
        height: 7rem !important;
    }


    .screenright{
        width: calc(100% + 1.5rem) !important; left: -1.2rem;position: relative;;
    }
    .screentop{
        height: calc(100% + 0.2rem) !important; top: -0.3rem;position: relative;
    }
    .screenbottom{
        height: calc(100% + 1.4rem) !important; top: -1.3rem;position: relative;
    }
}

/* PC端*/
#titlebarbg {
    height: 5rem;
    width: 30rem;
}

#logostyle {
    height: 2.5rem;
}




.screenright{
     width: calc(100% + 0.4rem) !important; left: -0.4rem;position: relative;;
}
.screentop{
    height: calc(100% + 0.2rem) !important; top: -0.1rem;position: relative;
}
.screenbottom{
    height: calc(100% + 0.4rem) !important; top: -0.4rem;position: relative;
}

datav的全屏容器我最后去掉了,只用到了里面的装饰。

参考笔记https://www.cnblogs.com/cjuan/p/9448981.htmlhttps://blog.csdn.net/syt_1013/article/details/127666848

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值