全屏适配pc端

1、安装插件

npm install --save lodash.debounce

2、封装组件pc .vue

<template>
  <div v-bind:style="styleObject" class="scale-box">
    <slot></slot>
  </div>
</template>

<script>
import debounce from "lodash.debounce"
export default {
  props: {
    width: {
      type: Number,
      default: 1920,
    },
    height: {
      type: Number,
      default: 1080,
    },
  },
  components: {},
  data() {
    return {
      scale: this.getScale(),
    }
  },
  computed: {
    styleObject(){
      let obj = {
        transform: `scale(${this.scale}) translate(-50%, -50%)`,
        WebkitTransform: `scale(${this.scale}) translate(-50%, -50%)`,
        width: this.width + "px",
        height: this.height + "px",
      }
       return obj
    }

  },
  mounted() {
    this.getScale()
    window.addEventListener("resize", this.setScale)
  },
  methods: {
    getScale() {
      // 固定好16:9的宽高比,计算出最合适的缩放比,宽高比可根据需要自行更改
      console.log(window.innerWidth,'window.innerWidth');
      let ww = window.innerWidth / this.width
      let wh = window.innerHeight / this.height
      return ww < wh ? ww : wh
    },

    setScale: debounce(function () {

      // 获取到缩放比,设置它
      let scale = this.getScale()
      this.scale = scale
       console.log(this.scale, "---")
    }, 500),
  },
  beforeDestroy() {
    window.addEventListener("resize", this.setScale)
  },
}
</script>

<style scoped lang="scss">
.scale-box {
  transform-origin: 0 0;
  position: absolute;
  left: 50%;
  top: 50%;
  transition: 0.3s;
}
</style>

3、使用刚才封装的pc

<template>
    <pc>
      <div class="fixing">
            <!-- 头部 -->
            <qheader></qheader>
            <el-main>
                <router-view></router-view>
            </el-main>
      </div>
   </pc>
</template>
<script>
import qheader from "@/components/head/index.vue"
// 引入
import pc from "@/components/pc.vue"
export default {
  name:"mainIndex",
  // 注册组件
  components: {
    qheader,
    pc
  },
  data() {
    return {
    };
  },
};
</script>

<style lang="scss">
@import "style.scss";
</style>

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code female artist

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值