当前位置: 首页 > article >正文

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


http://www.kler.cn/news/233918.html

相关文章:

  • (四)elasticsearch 源码之索引流程分析
  • 学习总结15
  • 【MySQL】数据库的基础——数据库的介绍、MySQL的介绍和架构、SQL分类、MySQL的基本使用、MySQL的存储引擎
  • Matlab使用点云工具箱进行点云配准ICP\NDT\CPD
  • 软件应用实例分享,电玩计时计费怎么算,佳易王PS5游戏计时器系统程序教程
  • 【工具】Android|Android Studio 长颈鹿版本安装下载使用详解
  • windows安装sqlite
  • C语言实现memcpy、memmove库函数
  • C++初阶:适合新手的手撕vector(模拟实现vector)
  • YOLOv5改进 | 融合改进篇 | 华为VanillaNet + BiFPN突破涨点极限
  • 探索Xposed框架:个性定制你的Android体验
  • go语言实现LRU缓存
  • Qt:QFileDialog
  • java Servlet 云平台教学系统myeclipse定制开发SQLServer数据库网页模式java编程jdbc
  • 【深度学习】:实验6布置,图像自然语言描述生成(让计算机“看图说话”)
  • 算法学习——LeetCode力扣双指针篇
  • LeetCode467. Unique Substrings in Wraparound String——动态规划
  • 图形学:Transform矩阵(3维 2维) 平移,旋转,缩放
  • 力扣738单调递增的数字思路以及贪心总结
  • centos 7.6 安装 openldap 2.5.17
  • Spring基础 - SpringMVC请求流程和案例
  • 图神经网络与图表示学习: 从基础概念到前沿技术
  • 【Linux】POSIX信号量基于环形队列的生产消费模型
  • Go基础知识学习-习题题解
  • 2024年度十余爆款爱心表白代码,还不进来瞅瞅?(一)
  • Git的基础操作指令
  • java大数据hadoop2.9.2 Flume安装操作
  • Jupyter Notebook如何在E盘打开
  • 机器学习系列——(十八)K-means聚类
  • Vue-56、Vue技术路由的使用