Vue中大屏适配和局部适配的方案总结

 更新时间:2025年03月23日 14:19:34   作者:码农Davi  
这篇文章主要为大家详细介绍了如何通过Vue.js的Mixins功能结合JavaScript实现页面内容的自适应缩放,以适应不同比例的屏幕,需要的小伙伴可以参考下

1.使用Mixins混入的方式解决自适应适配功能

通用的 css3:scale 缩放方案,通过 ref 指向页面,屏幕改变时缩放内容。项目的基准尺寸是 1920px*1080px,所以支持同比例屏幕 100%填充,如果非同比例则会自动计算比例居中填充,不足的部分则留白。

实现代码 screenmixin.js

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

// * 设计稿尺寸(px)
// const baseWidth = document.body.clientWidth;
// const baseHeight = document.body.clientHeight;
const baseWidth = 1920;
const baseHeight = 1080;

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

export default {
  data() {
    return {
      // * 定时函数
      drawTiming: null,
    };
  },
  mounted() {
    setTimeout(() => {
      this.calcRate();
    }, 200);
    window.addEventListener('resize', this.resize);
  },
  created() {},
  beforeDestroy() {
    window.removeEventListener('resize', this.resize);
  },
  methods: {
    calcRate() {
      const appRef = this.$refs['appRef'];
      if (!appRef) return;
      // 当前宽高比
      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%)`;
        }
      }
    },
    resize() {
      clearTimeout(this.drawTiming);
      this.drawTiming = setTimeout(() => {
        this.calcRate();
      }, 200);
    },
  },
};

页面使用

<template>
  <div ref="appRef" class="wrapper">
    <div class="home-canvas">
   		内容
    </div>
  </div>
</template>

<script>
import screenMinxi from '@/utils/screenmixin';
export default {
  mixins: [screenMinxi],
};
</script>

<style lang="scss" scoped>
// 必需写宽高,如有单位转换在style中写
.wrapper{
  width: 1920px;
  height: 1080px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transform-origin: left top; /* 缩放基点为左上角 */
  overflow: hidden;
}
</style>

2.局部适配方案 mixins.js

export const scaleMixin = {
  methods: {
    // 计算缩放比例
    getScaleRatio() {
      const baseWidth = 1920; // 基准宽度
      const baseHeight = 1080; // 基准高度
      const screenWidth = window.innerWidth; // 屏幕宽度
      const screenHeight = window.innerHeight; // 屏幕高度
      const ratioX = screenWidth / baseWidth;
      const ratioY = screenHeight / baseHeight;
      return Math.min(ratioX, ratioY); // 取最小比例作为缩放比例
    },
  },
  mounted() {
    // 监听窗口变化,重新计算缩放比例
    window.addEventListener('resize', () => {
      const scaleRatio = this.getScaleRatio();
      this.$refs.wrapper.style.transform = `scale(${scaleRatio})`;
    });
    // 初始化缩放比例
    const scaleRatio = this.getScaleRatio();
    this.$refs.wrapper.style.transform = `scale(${scaleRatio})`;
  },
};

引入使用

import { scaleMixin } from './mixins';
mixins: [scaleMixin],
 <div class="canvas-wrapper wrapper" style="width:1600px;heibht:890px;" ref="wrapper" >
      <div class="">内容</div>
 </div>

样式style

.wrapper {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  transform-origin: left top; /* 缩放基点为左上角 */
  transform: scale(1); /* 初始化缩放比例 */
}

到此这篇关于Vue中大屏适配和局部适配的方案总结的文章就介绍到这了,更多相关Vue大屏适配和局部适配内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue实现选项卡案例

    vue实现选项卡案例

    这篇文章主要为大家详细介绍了vue实现选项卡案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Vue.js实现可配置的登录表单代码详解

    Vue.js实现可配置的登录表单代码详解

    这篇文章主要介绍了Vue.js实现可配置的登录表单实例代码详解,文中给大家补充介绍了vue.js 全选与取消全选的实例代码,需要的朋友可以参考下
    2018-03-03
  • 在 Linux/Unix 中不重启 Vim 而重新加载 .vimrc 文件的流程

    在 Linux/Unix 中不重启 Vim 而重新加载 .vimrc 文件的流程

    这篇文章主要介绍了在 Linux/Unix 中不重启 Vim 而重新加载 .vimrc 文件的流程,需要的朋友可以参考下
    2018-03-03
  • Vue中路由参数与查询参数传递对比解析

    Vue中路由参数与查询参数传递对比解析

    在Vue.js中,路由与导航不仅涉及页面切换,还包括了向页面传递参数和获取查询参数,这篇文章主要介绍了Vue路由参数与查询参数传递,需要的朋友可以参考下
    2023-08-08
  • VUE和Antv G6实现在线拓扑图编辑操作

    VUE和Antv G6实现在线拓扑图编辑操作

    这篇文章主要介绍了VUE和Antv G6实现在线拓扑图编辑操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • vue2.x数组劫持原理的实现

    vue2.x数组劫持原理的实现

    这篇文章主要介绍了vue2.x数组劫持原理的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Vue.js计算属性computed与watch(5)

    Vue.js计算属性computed与watch(5)

    这篇文章主要为大家详细介绍了Vue.js计算属性computed与watch,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • vue 更改连接后台的api示例

    vue 更改连接后台的api示例

    今天小编就为大家分享一篇vue 更改连接后台的api示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • vue项目使用lodash节流防抖函数问题解决方案

    vue项目使用lodash节流防抖函数问题解决方案

    在lodash函数工具库中,防抖 _.debounce 和节流 _.throttle 函数在一些频繁触发的事件中比较常用,这篇文章主要介绍了vue项目使用lodash节流防抖函数问题与解决,需要的朋友可以参考下
    2023-10-10
  • 使用 webpack 插件自动生成 vue 路由文件的方法

    使用 webpack 插件自动生成 vue 路由文件的方法

    这篇文章主要介绍了使用 webpack 插件自动生成 vue 路由文件的方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08

最新评论