Vue大屏自适应缩放解决方案 = 使用transform:scale

在原文的基础上改进了 https://www.jianshu.com/p/dd0c1fd838c2

子组件

<script>
export default {
  props: {
    width: number,
    height: number,
    minWidth: number,
  },
  data() {
    return {
      scale: 0,
      realHeight: 0,
    }
  },
  mounted() {
    this.setScale()
    window.addEventListener('resize', this.debounce(this.setScale))
  },
  methods: {
    getScale() {
      const wh = window.innerHeight / this.height
      const ww = Math.max(this.minWidth, window.innerWidth) / this.width
      this.realHeight = `${this.height * ww}px`
      return ww
    },
    setScale() {
      // 获取到缩放比例,设置它
      this.scale = this.getScale()
      if (this.$refs.ScaleBox)
        this.$refs.ScaleBox.style.setProperty('--scale', this.scale)
    },
    debounce(fn, delay) {
      const delays = delay || 100
      let timer
      return function () {
        const that = this
        const args = arguments
        if (timer)
          clearTimeout(timer)

        timer = setTimeout(() => {
          timer = null
          fn.apply(that, args)
        }, delays)
      }
    },
  },
}
</script>

<template>
  <div
    style="width:100%;height:659px;overflow: hidden;" :style="{
      height: realHeight,
    }"
  >
    <div
      ref="ScaleBox" class="ScaleBox" :style="{
        width: `${width}px`,
        height: `${height}px`,
      }"
    >
      <slot />
    </div>
  </div>
</template>

<style>
#ScaleBox {
    --scale: 1;
}

.ScaleBox {
    transform: scale(var(--scale));
    transform-origin: 0 0;
    z-index: 999;
}
</style>

使用

    <div style="min-width:1200px;overflow: auto;">
    - -
    <ScaleBox :height="659" :width="1920" :min-width="1200">
      <div style="width:1920px">
        <img src="/image/about/about3.png" alt="">
      </div>
    </ScaleBox>
    - -
  </div>

效果

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 在Vue大屏设计中实现自适应的方法有多种。一种方法是通过配置文件设置大屏设计的尺寸,如将宽度设置为1920像素,高度设置为1080像素,并设置缩放比例为20。\[1\]另一种方法是在组件中使用样式来实现自适应。可以使用flex布局来实现垂直居中,并设置各个组件的宽度比例,如左侧组件宽度为410/96rem,中间组件宽度为1060/96rem,右侧组件宽度为450/96rem。\[2\]还可以使用ScaleBox组件来实现自适应,通过设置最小宽度为1200像素,并在ScaleBox组件内部包裹需要自适应的内容,如图片。\[3\]这些方法都可以根据不同的需求来实现Vue大屏自适应效果。 #### 引用[.reference_title] - *1* *2* [Vue 大屏可视化-屏幕自适应(保持设计尺寸比例)](https://blog.csdn.net/u011097323/article/details/105288458)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Vue大屏自适应缩放解决方案 = 使用transform:scale](https://blog.csdn.net/weixin_47663795/article/details/127615424)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值