vue版本大屏适配组件

起因

最近开发一个信息管理大屏,屏幕尽寸为2560*1080。
公司内部的有一些项目上对于大屏是直接定死宽度与高度的,这样在开发时,特别是针对这种特大屏,开发人员很难有整体感受。还有一些是进行了适配的处理,但是是针对于当前的项目进行处理的,并且还没有沉淀出工具可以直接复用。

适配方案

利用rem进行布局,最著名的就是淘宝的flexable布局。其核心的原理,就是更改更路径下的 font-size,然后,使用的大小全部从px转换为rem,这需要开发人员进行计算,还可以利用css预处理语言中(项目中使用是scss)的高级功能来实现px2rem。但是整体上来说,还是比较复杂的。

利用百分比,同样这里开发人员也就是需要进行计算的。

第三种,利用scale进行缩放处理,也就是本组件所使用的方式。

注意点

但是对于中间的区域 ,我们通过是使用加载模型,或者地图相关,此处是不能直接使用scale进行区域缩放的,只处理对宽高进行比例计算,它会自动适配。

核心技术

scale

scale是css3中的属性。一般情况下默认缩放中心点,是图形的中心点,但是在使用translate(-50%,-50%)时,需要将默认缩放中心点变为左上角。

transform:scale(0.5);
transform-origin:0 0;

css变量

css变量是可以由开发者进行自定义,必须要以 -- 开头的,然后利用var()函数在其它css属性中使用,它是对大写小敏感的。

element{
    --color:red;
}

div{
    color:var(--color)
}

如何利用js来操作css变量?
方式一:直接内联到元素中
document.body.style.setProperty(--color, 'red')

方式二:创建style 再来插入

const styleEl = documente.createElement('style')
styleEl.innerHTML = `
 :root{
     --color:red; 
 }
`
document.body.append(styleEl)

水平垂直居中

因为涉及到多层 所以可以采用定位的方式

<div class="parent">
<div class="son"></div>
</div>
<style>
.parent{
    position:relative;
    width:100%;
    height:100vh;
}
.son{
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
}
</style>

组件使用

    class="ssfc-screen"
    prop-name="ssfc-screen-scale"
    adpter-class="adpter-area"
    :width="width"
    :height="height"
    :resize-listenter="resizeListenter"
  >
    <cesium-main slot="main"></cesium-main>
    <div>
      <div v-show="ifShow">
        <top-bar></top-bar>
        <!--    左侧面板-->
        <left></left>
        <!--    右侧侧面板-->
        <right></right>
        <!--    底部 -->
        <bottom></bottom>
        <!-- ./staticData/imgs/allScreen.png -->
        <!-- 模型上其余部分 -->
        <remainingAreas class="reset-events"></remainingAreas>
      </div>

      <img
        :src="picUrl"
        alt=""
        class="reset-events"
        :class="ifShow ? 'imgFull' : 'imgPart'"
        @click="
          ifShow = !ifShow
          getFullScreen()
        "
      />
    </div>
  </AdpterScreen>
slot="main" 主区域不进行缩放  其它区域进行缩放
propName  css变量名 注意不需要加 --
adpterClass 适配类  可供弹框等使用
width height 设计稿宽高
resizeListenter risize事件监听里面回调参数为当前缩放值 

源码

<!--
 * @Description
 * @Autor 朱俊
 * @Date 2022-04-13 17:19:05
 * @LastEditors: wangs
 * @LastEditTime: 2022-04-14 18:03:24
-->
<template>
  <div class="big-screen-wrapper" ref="containerRef">
    <div class="main-wrapper" ref="mainRef">
      <slot name="main"></slot>
    </div>
    <div class="layer-wrapper" ref="layerRef">
      <slot />
    </div>
  </div>
</template>
<script>
import ScaleLayout from './scaleLayout'
export default {
  props: {
    propName: {
      type: String,
      default: 'scale' + new Date().getTime()
    },
    width: {
      type: Number,
      default: 1920
    },
    height: {
      type: Number,
      default: 1080
    },
    adpterClass: {
      type: String,
      default: 'apter-area'
    },
    resizeListenter: {
      type: Function,
      default: () => {
        return () => {}
      }
    }
  },
  data() {
    return {}
  },
  mounted() {
    this.$nextTick(() => {
      const container = document.body
      const mainEl = this.$refs['mainRef']
      const layerEl = this.$refs['layerRef']
      new ScaleLayout({
        context: this,
        propName: this.propName,
        width: this.width,
        height: this.height,
        container,
        mainEl,
        layerEl,
        adpterClass: this.adpterClass,
        resizeListenter: this.resizeListenter
      })
    })
  }
}
</script>

<style lang="scss" scoped>
.big-screen-wrapper {
  width: 100%;
  height: 100vh;
  position: relative;
  background: #000;
  overflow: hidden;
  .main-wrapper {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .layer-wrapper {
    position: absolute;
    left: 50%;
    top: 50%;
    overflow: hidden;

    pointer-events: none; // 图层事件穿透

    * {
      // 其它事件恢复
      pointer-events: auto;
    }
  }
}
</style>

/*
 * @Description
 * @Autor 朱俊
 * @Date 2022-04-13 17:19:27
 * @LastEditors 朱俊
 * @LastEditTime 2022-04-14 23:05:19
 */
// 防抖
function debounce(fn, t) {
  const delay = t || 500
  let timer
  return (...args) => {
    if (timer) {
      clearTimeout(timer)
    }
    const context = this
    timer = setTimeout(() => {
      timer = null
      fn.apply(context, args)
    }, delay)
  }
}

class ScaleLayout {
  constructor({
    context,
    width,
    height,
    container,
    propName,
    mainEl,
    layerEl,
    adpeterClass,
    resizeListenter
  }) {
    this.styleEl = null
    this.scale = 1 // 默认初始缩放1
    this.context = context
    this.width = width // 设计稿宽度
    this.height = height // 设计稿高度
    this.container = container || document.body
    this.propName = propName || 'scale'
    this.mainEl = mainEl
    this.layerEl = layerEl
    this.adpeterClass = adpeterClass || 'adpter-area'
    this.resizeListenter = resizeListenter || (() => {})
    this.dealLayout()
    this.setScale()
    this.resizeListenter(this.scale)
    this.listen()
  }

  // 处理布局
  dealLayout() {
    // 处理main区域
    this.mainEl.style = `
      width: calc(${this.width}px * var(--${this.propName}));
      height: calc(${this.height}px * var(--${this.propName}));
    `
    // 处理图层
    this.layerEl.style = `
     width: ${this.width}px;
     height: ${this.height}px;
     transform: scale(var(--${this.propName})) translate(-50%,-50%);
     transform-origin:0 0;
    `

    // 动态生成适配类
    this.styleEl = document.createElement('style')

    this.styleEl.innerHTML = `
     .${this.adpeterClass} {
      transform: scale(var(--${this.propName}));
      transform-origin:0 0;
     }
    `
    this.container.append(this.styleEl)
  }

  // 页面生命周期及浏览器大小监听
  listen() {
    this.onresize = debounce(() => {
      this.setScale()
      this.resizeListenter(this.scale)
    }, 500)
    window.addEventListener('resize', this.onresize)

    this.context.$on('hook:beforeDestroy', () => {
      window.removeEventListener('resize', this.onresize)
      this.styleEl && this.container.removeChild(this.styleEl)
    })
  }

  // 设置缩放
  dealScale() {
    const ws = window.innerWidth / this.width
    const hs = window.innerHeight / this.height
    return ws < hs ? ws : hs
  }
  //  获取scale值
  getScale() {
    return this.scale
  }
  // 设置scale
  setScale() {
    this.scale = this.dealScale()
    this.container.style.setProperty(`--${this.propName}`, this.scale)
  }
}

export default ScaleLayout
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容