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

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

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

组件的主要代码如下

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

相关推荐

  1. vue3 适应屏幕组件

    2024-02-11 12:50:01       29 阅读
  2. VUE适应

    2024-02-11 12:50:01       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-11 12:50:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-11 12:50:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-11 12:50:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-11 12:50:01       20 阅读

热门阅读

  1. [C#] 如何对列表,字典等进行排序?

    2024-02-11 12:50:01       28 阅读
  2. 【Linux】基于单例模式懒汉实现方式的线程池

    2024-02-11 12:50:01       25 阅读
  3. Shell之sed

    2024-02-11 12:50:01       32 阅读
  4. 【算法】【数据结构】算法与数据结构的关系

    2024-02-11 12:50:01       33 阅读
  5. mysql

    mysql

    2024-02-11 12:50:01      28 阅读
  6. Nginx配置php留档

    2024-02-11 12:50:01       27 阅读
  7. RuoYi模块功能分析:第八章定时任务

    2024-02-11 12:50:01       26 阅读
  8. P2036 [COCI2008-2009 #2] PERKET题解

    2024-02-11 12:50:01       28 阅读
  9. 学习数据结构和算法的第6天

    2024-02-11 12:50:01       28 阅读
  10. 设计模式-适配器模式 Adapter

    2024-02-11 12:50:01       28 阅读
  11. 应急响应-挖矿木马-常规处置方法

    2024-02-11 12:50:01       26 阅读