vue 根据缩放适配大屏

根据scale缩放适配大屏

// 这种适配只适合 1920*1080 和3840 * 2160的屏幕如果是超宽屏幕就往下看
// 不多逼逼 直接上代码 

<template>
  <div class="home" id="home" ref="home"></div>
</template>

<script>
export default {
  name: "Home",
  methods: {
    f() {
      var w = window.innerWidth; //获取窗口宽度
      var h = window.innerHeight;//获取窗口高度
      const w_w = this.$refs.home.offsetWidth;//获取元素宽度
      const h_h = this.$refs.home.offsetHeight;//获取元素高度
      let num = 0;
      w / h > w_w / h_h ? (num = h / h_h) : (num = w / w_w); 
      // 窗口宽/窗口高>元素宽/元素高
      home.style.transform = `translate(-50%, -50%) scale(${num})`;
    },
  },
  mounted() {
    this.f();
    window.addEventListener("resize", this.f);
  },
};
</script>

<style scoped>
.home {
  /* width: 7680px; */
  /* height: 2160px; */
  width: 1920px;
  height: 1080px;
  background-color: rgb(16, 179, 111);
  transform: translate(-50%, -50%);
  position: absolute;
  left: 50%;
  top: 50%;
  transition: all 1s;
}
</style>

根据scale缩放 超宽屏幕适配方案

<template>
  <div
    id="screen"
    :style="{
      width: `${style.width}px`,
      height: `${style.height}px`,
      transform: `${style.transform}`
    }"
  >
   <div class="box"></div>
  </div>
</template>
 
<script>
export default {
  name: 'ScaleBox',
  props: {},
  data () {
    return {
      style: {
        width: '7680',
        height: '2160',
        transform: 'scaleY(1) scaleX(1) translate(-50%, -50%)'
      }
    }
  },
  methods: {
    getScale () {
      const w = window.innerWidth / this.style.width
      const h = window.innerHeight / this.style.height
      return { x: w, y: h }
    },
    setScale () {
      const scale = this.getScale()
      this.style.transform =
        'scaleY(' + scale.y + ') scaleX(' + scale.x + ') translate(-50%, -50%)'
    }
  },
  mounted () {
    const that = this
    that.setScale()
    /* 窗口改变事件 */
    window.addEventListener('resize', function () {
      console.log('窗口发生变化')
      that.setScale()
    })
  }
}
</script>
 
<style scoped>
#screen {
  z-index: 100;
  transform-origin: 0 0;
  position: fixed;
  left: 50%;
  top: 50%;
  transition: 0.3s;
}
.box {
  width: 100%;
  height: 100%;
  background-color: green;

}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值