vue 响应式页面使用transform实现

为了在 Vue 应用中实现一个响应式页面缩放功能,使页面能够根据屏幕大小自动进行缩放。通过监听窗口大小变化事件,并结合节流函数和 resize 函数,实现了页面元素的动态缩放效果。

但是希望页面缩放后尽量保持原样,所以使用了两套UI,1920和1024,根据页面缩放监听resize动态添加类名进行实现的响应式,因此封装了scss函数

  1. 在utils下新建resize.js
js 复制代码
// 屏幕按比例缩放
export default function (pageW, pageH, id,_scale) {
  const el = document.getElementById(id)
  el.style.cssText += `
            ;position: absolute;
            left: 0;
            top: 0;
            width: ${pageW}px;
            height: ${pageH}px;
            transform-origin: left top;
        `
  return function () {
    if (!el) return false

    const originW = document.body.clientWidth
    const originH = document.body.clientHeight
    const origeScale = originW / originH
    const bgScale = pageW / pageH
    // const _scale = origeScale > bgScale ? (originH / pageH) : (windowWidth / pageW)
    el.style['-webkit-transform'] = `scale(${_scale})`
    el.style.transform = `scale(${_scale})`
  }
}
export const scaleFunc = (pageW, pageH) => {
const originW = document.body.clientWidth
const originH = document.body.clientHeight
const origeScale = originW / originH
const bgScale = pageW / pageH
return origeScale > bgScale ? (originH / pageH) : (originW / pageW)
}
  • pageW 表示页面宽度 当前设置的是设计稿1920的二倍
  • pageH 表示页面高度 当前设置的是设计稿1080的二倍
  • id 表示要缩放的元素的 id
  • _scale 表示缩放比例
  1. 在utils下新建index.js

index.js 文件中定义了一个名为 throttle 的函数,用于实现函数节流的功能。该函数接收两个参数:fn 表示要执行的函数,delay 表示延迟时间。在函数内部使用闭包和 setTimeout 来控制函数的执行频率。

js 复制代码
// 节流
export const throttle = (fn, delay = 200) => {
  let timer = null
  return function () {
    clearTimeout(timer)
    timer = setTimeout(function () {
      fn()
    }, delay)
  }
}
  1. 在main.js中引用
js 复制代码
import { throttle} from '@/utils/index'
import resize from "@/utils/resize";
changeScreen()
// 在屏幕大小变化时进行页面缩放操作
window.onresize = throttle(changeScreen)

function changeScreen () {
  setTimeout(() => {
    var windowWidth =
      document.documentElement.clientWidth || document.body.clientWidth
    var windowHeight =
      document.documentElement.clientHeight || document.body.clientHeight

    let resizeFunc
    var is360 = _mime('type', 'application/gameplugin')
    let widthVal = windowWidth * 2
    let heightVal = windowHeight * 2

    // if (is360) {
    //   if (isChrome()) {
    //     widthVal = windowWidth * 1.9
    //     heightVal = windowHeight * 1.9
    //   }
    // }
    resizeFunc = resize(widthVal, heightVal, 'app', windowWidth / widthVal)

    resizeFunc()
  }, 100)
}
  1. scss文件 在styles下新建mixins.scss
scss 复制代码
//默认设计稿的宽度
$designWidth: 3840;
//默认设计稿的高度
$designHeight: 2160;


//px转为vw的函数

@function vw($px) {
  // 进行计算操作
  $result: ($px / $designWidth * 2) * 100;
  $factor: 100000;
  $roundedValue: round($result * $factor);
  $roundedResult: $roundedValue / $factor;

  @return #{$roundedResult}vw;
  
}


//px转为vh的函数

@function vh($px) {
  $result: ($px / $designHeight * 2) * 100;
  $factor: 100000;
  $roundedValue: round($result * $factor);
  $roundedResult: $roundedValue / $factor;
  @return #{$roundedResult}vh;
}
@function min_vw($px) {
  $result: ($px / 2048 * 2) * 100;
  $factor: 100000;
  $roundedValue: round($result * $factor);
  $roundedResult: $roundedValue / $factor;

  @return #{$roundedResult}vw;
}
@function min_vh($px) {
  $result: ($px / 1536 * 2) * 100;
  $factor: 100000;
  $roundedValue: round($result * $factor);
  $roundedResult: $roundedValue / $factor;
  @return #{$roundedResult}vh;
}
  1. 在vue页面设置样式
scss 复制代码
// 1920页面使用的样式
.box {
  width: vw(700*2);  ---- 700为设计稿给的宽度
  height: vh(836*2);
  font-size: vw(14*2); ---- 14为设计稿给的字体大小
  border-radius: vw(8*2); ---- 8为设计稿给的圆角大小
  margin-bottom: vh(20 * 2);
}
// 1024页面使用的样式 --- 动态判断页面宽度添加类名
.box-width {
  width: min_vw(700*2);  ---- 700为设计稿给的宽度
  height: min_vh(836*2);
  font-size: min_vw(14*2); ---- 14为设计稿给的字体大小
  border-radius: min_vw(8*2); ---- 8为设计稿给的圆角大小
  margin-bottom: min_vh(20 * 2);
}

mixins.scss具体引用:详见该篇文章Vue项目切换主题颜色(mixin + scss)

相关推荐
随风一样自由5 分钟前
【前端领域】2026最新前端领域全梳理(框架/工具/AI/跨端/底层标准/就业趋势)
前端·人工智能·前端框架
这是个栗子5 分钟前
【前端性能优化】优化数据加载:用 Promise.all 从串行到并行
前端·javascript·性能优化·异步编程·前端优化·promise.all
fei_sun1 小时前
黑洞路由(Null Route/空接口路由)
服务器·前端·javascript
大爱一家盟1 小时前
告别卡点BGM同质化 2026原创卡点音乐素材下载网站 TOP5 推荐
大数据·前端·人工智能
彦为君1 小时前
算法思维与经典智力题
java·前端·redis·算法
aa小小2 小时前
localhost 访问异常排查笔记
前端
格子软件2 小时前
2026年GEO优化系统源码的分布式状态机深度拆解
java·前端·vue.js·vue·geo
陈随易2 小时前
Rust、Golang、MoonBit 编译成 WASM,体积和速度差距有多大?
前端·后端·程序员
IT_陈寒2 小时前
Python多线程的坑,我居然现在才踩到
前端·人工智能·后端
摇滚侠2 小时前
方法 A 等方法 B 执行完再执行 叫同步调用还是异步调用 JS 默认是同步调用还是异步调用
开发语言·javascript·ecmascript