天天看點

vue大屏展示适配

1.utils檔案夾建一個檔案cv以下代碼

export function useIndex (appRef) {
  // * appRef指向最外層容器

  // * 定時函數
  let timer = null
  // * 預設縮放值
  const scale = {
    width: '1',
    height: '1'
  }
  // * 設計稿尺寸(px)
  const baseWidth = 1920
  const baseHeight = 1080

  // * 需保持的比例(預設2)
  // const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))
  const baseProportion = 2
  const calcRate = () => {
    // 目前寬高比
    const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
    if (appRef) {
      if (currentRate > baseProportion) {
        // 表示更寬
        scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
        scale.height = (window.innerHeight / baseHeight).toFixed(5)
        appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
      } else {
        // 表示更高
        scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
        scale.width = (window.innerWidth / baseWidth).toFixed(5)
        appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
      }
    }
  }

  const resize = () => {
    clearTimeout(timer)
    timer = setTimeout(() => {
      calcRate()
    }, 200)
  }

  // 改變視窗大小重新繪制
  const windowDraw = () => {
    window.addEventListener('resize', resize)
  }

  return {
    appRef,
    calcRate,
    windowDraw
  }
}
           

2.app.vue結構樣式

<template>
  <div ref="appRef" class="main">
    <div class="layout-container">
    </div>
  </div>
</template>

<script>
import { useIndex } from '@/utils/utilsDram.js'
export default {
  mounted () {
    const { calcRate, windowDraw } =useIndex(this.$refs.appRef)
    calcRate()
    windowDraw()
  }
}
</script>
<style lang="scss" scoped>
.main {
  color: #d3d6dd;
  width: 1920px;
  height: 1080px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transform-origin: left top;

  .layout-container {
    width: 100%;
    height: 100%;
    }
   }
  </style>
           

3.字型大小盒子寬度直接設定px不管放大縮小都是最初的樣子,基本頁面大小不會相差太遠,下圖是頁面放大500倍的效果

vue大屏展示适配
上一篇: this的指代

繼續閱讀