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


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

    一、效果图

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

    图一:缩小的效果

    图二:放大的效果

    二、使用方式
    1. <template>
    2. <div id="bigScreenContainer" ref="bigScreenContainer">
    3. // 内容
    4. div>
    5. template>
    6. <script>
    7. // import { bigScreenAdapterMixin } from "snows-utils";
    8. import bigScreenAdapterMixin from "@/utils/bigAdapterMixin";
    9. export default {
    10. name:'',
    11. mixins: [bigScreenDdapterMixin],
    12. script>

    三、实现代码
    1. /*
    2. * @Description: ------------ fileDescription -----------
    3. * @Author: snows_l snows_l@163.com
    4. * @Date: 2023-11-16 09:35:24
    5. * @LastEditors: snows_l snows_l@163.com
    6. * @LastEditTime: 2023-11-16 10:47:15
    7. * @FilePath: /source-snows-utils/utils/bigAdapterMixin.js
    8. */
    9. // 屏幕适配 mixin 函数
    10. // * 默认缩放值
    11. const scale = {
    12. width: '1',
    13. height: '1'
    14. };
    15. // * 设计稿尺寸(px)
    16. const baseWidth = 1920;
    17. const baseHeight = 1080;
    18. // * 需保持的比例(默认1.77778)
    19. const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5));
    20. export default {
    21. data() {
    22. return {
    23. // * 定时函数
    24. drawTiming: null
    25. };
    26. },
    27. mounted() {
    28. this.calcRate();
    29. window.addEventListener('resize', this.resize);
    30. },
    31. beforeDestroy() {
    32. window.removeEventListener('resize', this.resize);
    33. },
    34. methods: {
    35. calcRate() {
    36. const style = {
    37. width: '1920px',
    38. height: '1080px',
    39. position: 'absolute',
    40. top: '50%',
    41. left: '50%',
    42. transformOrigin: 'left top'
    43. };
    44. const bigScreenContainer = document.getElementById('bigScreenContainer');
    45. if (!bigScreenContainer) return;
    46. // 设置必要样式(上下左右居中)
    47. bigScreenContainer.style.width = style.width;
    48. bigScreenContainer.style.height = style.height;
    49. bigScreenContainer.style.position = style.position;
    50. bigScreenContainer.style.top = style.top;
    51. bigScreenContainer.style.left = style.left;
    52. bigScreenContainer.style.transformOrigin = style.transformOrigin;
    53. // 当前宽高比
    54. const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5));
    55. if (bigScreenContainer) {
    56. if (currentRate > baseProportion) {
    57. // 表示更宽
    58. scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5);
    59. scale.height = (window.innerHeight / baseHeight).toFixed(5);
    60. bigScreenContainer.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
    61. } else {
    62. // 表示更高
    63. scale.height = (window.innerWidth / baseProportion / baseHeight).toFixed(5);
    64. scale.width = (window.innerWidth / baseWidth).toFixed(5);
    65. bigScreenContainer.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
    66. }
    67. }
    68. },
    69. resize() {
    70. clearTimeout(this.drawTiming);
    71. this.drawTiming = setTimeout(() => {
    72. this.calcRate();
    73. }, 200);
    74. }
    75. }
    76. };

    四:也可以直接安装 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

    1. <template>
    2. <div id="bigScreenContainer" ref="bigScreenContainer">
    3. // 内容
    4. div>
    5. template>
    6. <script>
    7. import { bigScreenAdapterMixin } from "snows-utils";
    8. // import bigScreenAdapterMixin from "@/utils/bigadapterMixin";
    9. export default {
    10. name:'',
    11. mixins: [bigScreenAdapterMixin],
    12. script>

  • 相关阅读:
    Strategies to Improve Signal-to-Noise Ratio in Communication
    TCP协议详解
    (185)Verilog HDL:设计一个移位功能Lfsr5
    集火全屋智能“后装市场”,真正玩得转的没几个
    C //习题 6.7 输出“魔方阵”。所谓魔方阵是指这样的方阵,它的每一行、每一列和对角线之和均相等。
    统计字符串中单词的个数--空格间隔
    Linux(Centos7版本)安装docker 使用官方安装脚本,一键安装docker 发生报错解决方法
    Linux项目自动化构建工具-make/Makefile
    从硬件“卷”到UI交互,车企怎样才能掌握智能化「灵魂」
    这几类外贸订单不要随意接
  • 原文地址:https://blog.csdn.net/snows_l/article/details/134441247