vue前端PC网页端适配

一:写一个js文件,drawMixin.js

// 屏幕适配 mixin 函数
 
// * 默认缩放值
const scale = {
    width: '1',
    height: '1',
  }
   
  // * 设计稿尺寸(px)
  const baseWidth = 1920
  const baseHeight = 1080
   
  // * 需保持的比例(默认1.77778)
  const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))
   
  export default {
    data() {
      return {
        // * 定时函数
        drawTiming: null
      }
    },
    mounted () {
        // console.log(window.screen.width/1960)
      //进入触发
      this.calcRate()
      window.addEventListener('resize', this.resize)
    },
    beforeDestroy () {
      window.removeEventListener('resize', this.resize)
    },
    methods: {
      calcRate () {
          // const appRef = this.$refs["appRef"]
          // appRef.style.transform=scale(window.screen.width/1960)
          // appRef.style.top=0;
          // appRef.style.left=0;
        setTimeout(()=>{
          //拿到整个页面元素
        const appRef = this.$refs["appRef"]
        //如果没有值就结束
        if (!appRef) return 
        // 当前宽高比
        // const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
        const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
        //判断:如果有值代表页面变化了
        if (appRef) {
          //判断当前宽高比例是否大于默认比例
          // if (currentRate < baseProportion) {
            // 如果大于代表更宽了,就是放大了
            //那么把默认缩放的宽高改为:同比例放大
            scale.width = (window.innerWidth/1920).toFixed(5)
            // scale.height = (window.innerWidth / baseHeight).toFixed(5)
            // console.log(scale.width,scale.height,'放大');
            //整个页面的元素样式,缩放宽高用当前同比例放大的宽高
            appRef.style.transform = `scale(${scale.width},${scale.width})`
            // appRef.style.position='absolute'
          // } else {
            // 如果不大于默认比例代表缩小了。
            //那么把默认缩放的宽高改为:同比例缩小
          //   scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
          //   scale.width = (window.innerWidth / baseWidth).toFixed(5)
          //   console.log(scale.width,scale.height,'缩小');
          //   //整个页面的元素样式,缩放宽高用当前同比例放大的宽高
          //   appRef.style.transform = `scale(${scale.width}, ${scale.height})`
          //   // appRef.style.position='absolute'
          // }
        }
        },0)
      },
      resize () {
        clearTimeout(this.drawTiming)
        this.drawTiming = setTimeout(() => {
          this.calcRate()
        }, 200)
      }
    },
  }
二:div 引用
  <div class="box"  ref="appRef">
三:vue文件引用
import drawMixin from "@/static/util/drawMixin.js";
export default {
  mixins: [drawMixin],

  name: "overview",

  },

四:盒子宽高设置

.box {
  width: 1920px;
  height: 1080px;
  background-image: url("../components/screenHeader/images/bg.png");
  display: flex;
  box-sizing: content-box;

  // position: relative;
  // flex-direction: column;
  // background: rgba(25, 29, 34, 0.65);
  //  z-index: 10;
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值