大屏/门户页面兼容各种分辨率或电脑缩放

需求要求: 需要支持缩放功能(缩放后 页面各元素模块正常展示)、 需要适配各种分辨率(初始加载不应出现横向滚动条)

选择的实现方案

利用 zoom 或者 transform 来缩放兼容页面样式,不动业务模块代码

    const isMobile = /iPhone|iPad|iPod|Android|Harmony/i.test(navigator.userAgent) // 手机端
    const canUseZoom = !isMobile && !/Firefox|KaiOS|Opera/i.test(navigator.userAgent) // 能使用 zoom 的浏览器
    this.getScaleListener(canUseZoom)

    getScaleListener(canUseZoom) {
      const devicewidth = document.documentElement.clientWidth
      if (devicewidth === 1920) return // 根据设计稿实际强开来
      // 如果需要以高度为标准的话可以打开下面注释的
      // const deviceheight = document.documentElement.clientHeight
      const scaleX = devicewidth / 1920
      // const scaleY = deviceheight / 919
      // const scale = Math.min(scaleX, scaleY)
      let scale = +scaleX.toFixed(4)
      document.body.style.setProperty(`--zoom`, scale)
      let myStyle = document.createElement('STYLE')
      myStyle.setAttribute('id', 'myScaleStyle')
      const minHeight = 100 / scale
      let styleTextNode = document.createTextNode(`
        #app {
          width: 1920px;
          margin: 0 auto;
          box-sizing: border-box;
          ${canUseZoom ? `
            zoom: var(--zoom)
            ` : `
            position: absolute;
            left: 50%;
            transform: scale(var(--zoom)) translateX(-50%);
            transform-origin: 0 0;
          `}
        }
        ${canUseZoom ? `
          .el-tooltip__popper, .el-popper {
            zoom: var(--zoom);
          }
          ` : `.el-tooltip__popper, .el-popper {
            transform: scale(var(--zoom));
        }`}
        #app {
          min-height: ${minHeight}vh;
        }
      `)
      myStyle.appendChild(styleTextNode)
      document.head.appendChild(myStyle)
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

#老程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值