0


vue项目实现自适应屏幕分辨率

npm install postcss-px2rem px2rem-loader --save

在根目录src中新建utils目录下新建rem.js等比适配文件

// rem等比适配配置文件
// 基准大小
const baseSize = 16
// 设置 rem 函数
function setRem () {
  // 当前页面屏幕分辨率相对于 1920宽的缩放比例,可根据自己需要修改
  const scale = document.documentElement.clientWidth / 1920
  // 设置页面根节点字体大小(“Math.min(scale, 2)” 指最高放大比例为2,可根据实际业务需求调整)
  document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
// 初始化
setRem()
// 改变窗口大小时重新设置 rem
window.onresize = function () {
  setRem()
}

3、在main.js中引入适配文件

import "./utils/rem.js"; // 自适应分辨率

4、到vue.config.js中配置插件

// 引入等比适配插件
const px2rem = require('postcss-px2rem')

// 配置基本大小
const postcss = px2rem({
  // 基准大小 baseSize,需要和rem.js中相同
  remUnit: 16
})

// 使用等比适配插件
module.exports = {
  lintOnSave: true,
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          postcss
        ]
      }
    }
  }
}

*********************************** 第二种(大屏可用)***********************************

在外层文件中(包含所有的子组件)

      onWindowResize() {
                const w = 1920
                const h = 1080
                const scaleW = document.body.clientWidth / w
                const scaleH = document.body.clientHeight / h
                const appDom = document.querySelector(".main") || null
                console.log(appDom)
                appDom.style.cssText = `transform: scale(${scaleW})`
                // appDom.style.cssText = `transform: scale(${scaleW}, ${scaleH})`;
            }

       mounted() {
            this.onWindowResize()
            setTimeout(() => {
                this.onWindowResize()
            }, 100)
        },

   created() {
            // 设置每隔 3minute 更新页面数据
            window.setInterval(() => {
                this.timer = setTimeout(() => {
                    this.reLoadPage();
                }, 0)
            }, 1000 * 60 * 3);
            window.addEventListener('resize', this.onWindowResize)
        },

        destroyed() {
            window.removeEventListener('resize', this.onWindowResize)
        }

本文转载自: https://blog.csdn.net/m0_74149462/article/details/130563922
版权归原作者 毛三仙 所有, 如有侵权,请联系我们删除。

“vue项目实现自适应屏幕分辨率”的评论:

还没有评论