vue3 可视化大屏自适应屏幕组件

首先定义了一个名叫ScreenContainerOptions的组件,需要传的参数如下

TypeScript 复制代码
export type ScreenContainerOptions = {
  width?: string | number
  height?: string | number
  screenFit?: boolean // 是否开启屏幕自适应,不然会按比例显示
}

组件的主要代码如下

TypeScript 复制代码
onMounted(async () => {
  await initSize()
  updateSize()
  updateScale()
  window.addEventListener('resize', onResize)
  isReady.value = true // 执行完上面的方法后再渲染slot插槽
})

// 初始化宽高
const initSize = () => {
  return new Promise((resolve) => {
    nextTick(() => {
      dom = refName.value
      parentDom = refNameParent.value

      // 获取大屏的真实尺寸(不传值就是dom元素的宽高)
      widthRef.value = props.options?.width || dom.clientWidth
      heightRef.value = props.options?.height || dom.clientHeight

      // 获取屏幕尺寸,避免重复计算
      if (!screenWidthRef.value || !screenHeightRef.value) {
        screenWidthRef.value = window.screen.width
        screenHeightRef.value = window.screen.height
      }

      resolve(true)
    })
  })
}
// 更新宽高
const updateSize = () => {
  dom.style.width = `${widthRef.value || screenWidthRef.value}px`
  dom.style.height = `${heightRef.value || screenHeightRef.value}px`
}
// 更新缩放比例
const updateScale = () => {
  // window.innerWidth 获取当前展示区域的宽度
  const currentWidth = window.innerWidth

  // 获取大屏最终真实的宽度
  const realWidth = widthRef.value || screenWidthRef.value

  // 是否开启屏幕适配,不会按照比例
  const { screenFit } = props.options
  // 如果不想屏幕留白,而是自适应宽高的话
  let heightScale = 1
  // window.innerWidth 获取当前展示区域的宽度
  const currentHeight = window.innerHeight
  // 获取大屏最终真实的宽度
  const realHeight = heightRef.value || heightRef.value
  if (screenFit) {
    heightScale = currentHeight / realHeight
    // if (parentDom) {
    //   parentDom.style.height = dom.style.height = `${window.innerHeight}px` // 父容器宽度设置为原屏幕的宽度
    // }
  }

  // 算出缩放比例并赋值
  // 只需要根据宽度计算即可
  const scale = currentWidth / realWidth
  dom && (dom.style.transform = `scale(${scale}, ${screenFit ? heightScale : 1})`) // 不开启screenFit的话高度不需要缩放
  if (parentDom) {
    parentDom.style.width = `${window.innerWidth}px` // 父容器宽度设置为原屏幕的宽度
    screenFit && (parentDom.style.height = `${window.innerHeight}px`) // 父容器宽度设置为原屏幕的宽度
  }
}

// 浏览器resize事件触发回调
const onResize = async () => {
  await initSize()
  await nextTick()
  updateScale()
}

组件完整代码地址

https://github.com/jimchou-h/vue-study/blob/dev/src/components/ScreenContainer.vue

相关推荐
招风的黑耳5 天前
Axure精选各类组件案例集锦:设计灵感与实战技巧
axure·数据可视化·可视化大屏·原型设计·元件库·可视化元件
一雨方知深秋6 天前
Element-plus安装及其基础组件使用
javascript·css·html·vue3·element-plus
清晨-阳光zx6 天前
vue-i18n在使用$t时提示类型错误
vue3·vue-i18n
陈逸子风6 天前
从0到1搭建权限管理系统系列四 .net8 中Autofac的使用(附源码)
vue3·webapi·权限·流程·表单
苍风的心上人7 天前
uniapp 使用Vue3 setup引入 uniapp 的onReachBottom
uni-app·vue3
wocwin7 天前
Vue3 + element-plus el-table二次封装组件新增虚拟滚动功能
vue.js·vue3·el-table·element-plus·virtual·虚拟滚动·table虚拟列表
Mao.O8 天前
在线聊天室项目(Vue3 + SpringBoot)
spring boot·websocket·vue3·在线聊天室
流氓也是种气质 _Cookie10 天前
14 vue3之内置组件trastion全系列
vue3·gsap
这孩子叫逆10 天前
uni-app进行微信小程序开发,快速上手
微信小程序·vue3·uniapp·vant
wocwin11 天前
vue3+element-plus icons图标选择组件封装
vue3·element-plus·组件封装·icons-vue·element-plus图标·图标选择组件·icon组件