可視化大屏scale適配方案

通過(guò)設(shè)置縮放比例,完成全屏適配

由于需要固定尺寸,所以需要內(nèi)部子控件用定位實(shí)現(xiàn)布局。
需要在body中設(shè)置定位

body {
  position: relative;
  width: 1920px;
  height: 1080px;
  transform-origin: left top;
}

固定比例適配

import { onMounted, onUnmounted } from "vue";
import _ from "lodash";

/**
  大屏適配的 hooks
 */
export default function useScalePage(
  targetWidth = 1920,
  targetHeight = 1080,
  targetProportion = 16 / 9
) {
  const resizeFunc = _.throttle(function () {
    triggerScale(); // 動(dòng)畫縮放網(wǎng)頁(yè)
  }, 100);

  onMounted(() => {
    triggerScale(); // 動(dòng)畫縮放網(wǎng)頁(yè)
    window.addEventListener("resize", resizeFunc);
  });

  onUnmounted(() => {
    window.removeEventListener("resize", resizeFunc); // 釋放
  });

  // 大屏的適配
  function triggerScale() {
    // 1.設(shè)計(jì)稿的尺寸
    let targetX = targetWidth;
    let targetY = targetHeight;
    let targetRatio = targetProportion; // 寬高比率

    // 2.拿到當(dāng)前設(shè)備(瀏覽器)的寬度
    let currentX =
      document.documentElement.clientWidth || document.body.clientWidth;
    let currentY =
      document.documentElement.clientHeight || document.body.clientHeight;

    // 3.計(jì)算縮放比例
    let scaleRatio = currentX / targetX; // 參照寬度進(jìn)行縮放 ( 默認(rèn)情況 )
    let currentRatio = currentX / currentY; // 寬高比率

    // 超寬屏
    if (currentRatio > targetRatio) {
      // 4.開始縮放網(wǎng)頁(yè)
      scaleRatio = currentY / targetY; // 參照高度進(jìn)行縮放
      document.body.style = `width:${targetX}px; height:${targetY}px;transform: scale(${scaleRatio}) translateX(-50%); left: 50%`;
    } else {
      // 4.開始縮放網(wǎng)頁(yè)
      document.body.style = `width:${targetX}px; height:${targetY}px; transform: scale(${scaleRatio})`;
    }
  }
}

等寬適配

import { onMounted, onUnmounted } from "vue";
import _ from "lodash";

/**
  大屏適配的 hooks
 */
export default function useScalePageAllWidth() {
  const resizeFunc = _.throttle(function () {
    triggerScale(); // 動(dòng)畫縮放網(wǎng)頁(yè)
  }, 100);

  onMounted(() => {
    triggerScale(); // 動(dòng)畫縮放網(wǎng)頁(yè)
    window.addEventListener("resize", resizeFunc);
  });

  onUnmounted(() => {
    window.removeEventListener("resize", resizeFunc); // 釋放
  });

  // 大屏的適配
  // 用于執(zhí)行縮放和拉伸的函數(shù)
  function triggerScale(targetWidth = 1920, targetHeight = 1080) {
    const screenWidth = document.documentElement.clientWidth;
    const scaleX = screenWidth / targetWidth;
    // 設(shè)置 body 元素的樣式
    document.body.style.width = targetWidth + "px";
    document.body.style.height = targetHeight + "px";
    document.body.style.transform = `scale(${scaleX})`;
    document.body.style.transformOrigin = "top left";
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容