分辨率适配

49 阅读1分钟

传统的rem适配对于简单的移动端应用较为适用,对于pc端应用,且要求兼容1360×768这种类型屏幕的效果并不好。

此时一般有两种方案:

zoom适配:

zoom用于缩放元素的内容,一般给最外层元素添加上即可:

const percent = window.screen.width / 1920
const zoom = 1 / percent
document.body.style.zoom = percent

这种方案对于一些中后台项目较为合适,但是其会影响如charts的toolTip的位置,map的点击位置等,所以一般的有图表展示类的项目不建议使用

transform适配:

这种方案不会造成额外的影响

const handlerResize = (
  width = 1920,
  height = 1080,
  element = document.documentElement
) => {
  const zoomWidth = window.screen.width / width;
  const zoomHeight = window.screen.height / height;
  var sizeWidth = 100 / zoomWidth + "%";
  var sizeHeight = 100 / zoomHeight + "%";
  element.style.transform =
    "scaleX(" + zoomWidth + ") scaleY(" + zoomHeight + ")";
  element.style.transformOrigin = "0 0";
  element.style.height = sizeHeight;
  element.style.width = sizeWidth;
};