vue固定比例16:9自适应方法(vue2,vue3)

提要:

主要是针对页面宽高进行监听,检测最短边,并以最短边去适配16:9比例的页面元素。通常用于pc需要适配大屏和一些后台管理方面的项目。

vue2写法

<template>
  <div class="container">
    <div class="content" :style="getAspectRatioStyle">
      <!-- 数据展示内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      contentWidth: 0,
      contentHeight: 0
    };
  },
  mounted() {
    this.calculateAspectRatio();
    window.addEventListener('resize', this.calculateAspectRatio);
  },
  beforeUnmount() {
    window.removeEventListener('resize', this.calculateAspectRatio);
  },
  computed: {
    getAspectRatioStyle() {
      return {
        width: `${this.contentWidth}px`,
        height: `${this.contentHeight}px`,
        margin: 'auto',
        background: 'white'
      };
    }
  },
  methods: {
    calculateAspectRatio() {
      const container = document.querySelector('.container');
      const containerWidth = container.offsetWidth;
      const containerHeight = container.offsetHeight;

      const aspectRatio = 16 / 9; // 16:9 比例
      const containerAspectRatio = containerWidth / containerHeight;

      if (containerAspectRatio > aspectRatio) {
        // 以高度为基准,按比例计算宽度
        this.contentHeight = containerHeight;
        this.contentWidth = Math.floor(containerHeight * aspectRatio);
      } else {
        // 以宽度为基准,按比例计算高度
        this.contentWidth = containerWidth;
        this.contentHeight = Math.floor(containerWidth / aspectRatio);
      }
    }
  }
};
</script>

<style>
.container {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.content {
  /* 根据计算得到的宽高样式设置 */
}
</style>

vue3写法

<template>
  <div class="container">
    <div class="content" :style="getAspectRatioStyle">
      <!-- 数据展示内容 -->
    </div>
  </div>
</template>

<script>
import { ref, onMounted, onBeforeUnmount, computed } from 'vue';

export default {
  setup() {
    const contentWidth = ref(0);
    const contentHeight = ref(0);

    const calculateAspectRatio = () => {
      const container = document.querySelector('.container');
      const containerWidth = container.offsetWidth;
      const containerHeight = container.offsetHeight;

      const aspectRatio = 16 / 9; // 16:9 比例
      const containerAspectRatio = containerWidth / containerHeight;

      if (containerAspectRatio > aspectRatio) {
        // 以高度为基准,按比例计算宽度
        contentHeight.value = containerHeight;
        contentWidth.value = Math.floor(containerHeight * aspectRatio);
      } else {
        // 以宽度为基准,按比例计算高度
        contentWidth.value = containerWidth;
        contentHeight.value = Math.floor(containerWidth / aspectRatio);
      }
    };

    onMounted(() => {
      calculateAspectRatio();
      window.addEventListener('resize', calculateAspectRatio);
    });

    onBeforeUnmount(() => {
      window.removeEventListener('resize', calculateAspectRatio);
    });

    const getAspectRatioStyle = computed(() => ({
      width: `${contentWidth.value}px`,
      height: `${contentHeight.value}px`,
      margin: 'auto',
      background: 'white'
    }));

    return {
      getAspectRatioStyle
    };
  }
};
</script>

<style>
.container {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.content {
  /* 根据计算得到的宽高样式设置 */
}
</style>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值