vue2 mixin的方式 大屏适配(缩放居中的方式)

使用要求:指定容器的 id 为 bigScreenContainer

一、效果图

不管你的屏幕多大都会根据设计稿 1920*1080 进行缩放

图一:缩小的效果

图二:放大的效果

二、使用方式
<template>
  <div id="bigScreenContainer" ref="bigScreenContainer">
    // 内容
  </div>
</template>



<script>
// import { bigScreenAdapterMixin } from "snows-utils";
import bigScreenAdapterMixin from "@/utils/bigAdapterMixin";
export default {
  name:'',
  mixins: [bigScreenDdapterMixin],
</script>

三、实现代码
/*
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2023-11-16 09:35:24
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2023-11-16 10:47:15
 * @FilePath: /source-snows-utils/utils/bigAdapterMixin.js
 */
// 屏幕适配 mixin 函数

// * 默认缩放值
const scale = {
  width: '1',
  height: '1'
};

// * 设计稿尺寸(px)
const baseWidth = 1920;
const baseHeight = 1080;

// * 需保持的比例(默认1.77778)
const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5));

export default {
  data() {
    return {
      // * 定时函数
      drawTiming: null
    };
  },
  mounted() {
    this.calcRate();
    window.addEventListener('resize', this.resize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resize);
  },
  methods: {
    calcRate() {
      const style = {
        width: '1920px',
        height: '1080px',
        position: 'absolute',
        top: '50%',
        left: '50%',
        transformOrigin: 'left top'
      };
      const bigScreenContainer = document.getElementById('bigScreenContainer');
      if (!bigScreenContainer) return;

      // 设置必要样式(上下左右居中)
      bigScreenContainer.style.width = style.width;
      bigScreenContainer.style.height = style.height;
      bigScreenContainer.style.position = style.position;
      bigScreenContainer.style.top = style.top;
      bigScreenContainer.style.left = style.left;
      bigScreenContainer.style.transformOrigin = style.transformOrigin;

      // 当前宽高比
      const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5));
      if (bigScreenContainer) {
        if (currentRate > baseProportion) {
          // 表示更宽
          scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5);
          scale.height = (window.innerHeight / baseHeight).toFixed(5);
          bigScreenContainer.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);
          bigScreenContainer.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
        }
      }
    },
    resize() {
      clearTimeout(this.drawTiming);
      this.drawTiming = setTimeout(() => {
        this.calcRate();
      }, 200);
    }
  }
};

四:也可以直接安装 snows-utils 工具直接在里面引用

        snows-utils 中包含了很多使用的方法

一些常用的工具函数(snows-utils),已发npm,会陆续更新-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/snows_l/article/details/131384116?spm=1001.2014.3001.5501

<template>
  <div id="bigScreenContainer" ref="bigScreenContainer">
    // 内容
  </div>
</template>



<script>
import { bigScreenAdapterMixin } from "snows-utils";
// import bigScreenAdapterMixin from "@/utils/bigadapterMixin";
export default {
  name:'',
  mixins: [bigScreenAdapterMixin],
</script>

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snows_l

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值