vue版本大屏適配組件

起因

最近開發(fā)一個信息管理大屏瘪匿,屏幕盡寸為2560*1080。
公司內(nèi)部的有一些項(xiàng)目上對于大屏是直接定死寬度與高度的透绩,這樣在開發(fā)時翘骂,特別是針對這種特大屏,開發(fā)人員很難有整體感受帚豪。還有一些是進(jìn)行了適配的處理碳竟,但是是針對于當(dāng)前的項(xiàng)目進(jìn)行處理的,并且還沒有沉淀出工具可以直接復(fù)用狸臣。

適配方案

利用rem進(jìn)行布局莹桅,最著名的就是淘寶的flexable布局。其核心的原理烛亦,就是更改更路徑下的 font-size,然后诈泼,使用的大小全部從px轉(zhuǎn)換為rem,這需要開發(fā)人員進(jìn)行計(jì)算煤禽,還可以利用css預(yù)處理語言中(項(xiàng)目中使用是scss)的高級功能來實(shí)現(xiàn)px2rem铐达。但是整體上來說,還是比較復(fù)雜的檬果。

利用百分比瓮孙,同樣這里開發(fā)人員也就是需要進(jìn)行計(jì)算的唐断。

第三種,利用scale進(jìn)行縮放處理杭抠,也就是本組件所使用的方式脸甘。

注意點(diǎn)

但是對于中間的區(qū)域 ,我們通過是使用加載模型偏灿,或者地圖相關(guān)丹诀,此處是不能直接使用scale進(jìn)行區(qū)域縮放的,只處理對寬高進(jìn)行比例計(jì)算翁垂,它會自動適配忿墅。

核心技術(shù)

scale

scale是css3中的屬性。一般情況下默認(rèn)縮放中心點(diǎn)沮峡,是圖形的中心點(diǎn)疚脐,但是在使用translate(-50%,-50%)時,需要將默認(rèn)縮放中心點(diǎn)變?yōu)樽笊辖恰?/p>

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

css變量

css變量是可以由開發(fā)者進(jìn)行自定義邢疙,必須要以 -- 開頭的棍弄,然后利用var()函數(shù)在其它c(diǎn)ss屬性中使用,它是對大寫小敏感的疟游。

element{
    --color:red;
}

div{
    color:var(--color)
}

如何利用js來操作css變量呼畸?
方式一:直接內(nèi)聯(lián)到元素中
document.body.style.setProperty(--color, 'red')

方式二:創(chuàng)建style 再來插入

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

水平垂直居中

因?yàn)樯婕暗蕉鄬?所以可以采用定位的方式

<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>
        <!--    左側(cè)面板-->
        <left></left>
        <!--    右側(cè)側(cè)面板-->
        <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" 主區(qū)域不進(jìn)行縮放  其它區(qū)域進(jìn)行縮放
propName  css變量名 注意不需要加 --
adpterClass 適配類  可供彈框等使用
width height 設(shè)計(jì)稿寬高
resizeListenter risize事件監(jiān)聽里面回調(diào)參數(shù)為當(dāng)前縮放值 

源碼

<!--
 * @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; // 圖層事件穿透

    * {
      // 其它事件恢復(fù)
      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 // 默認(rèn)初始縮放1
    this.context = context
    this.width = width // 設(shè)計(jì)稿寬度
    this.height = height // 設(shè)計(jì)稿高度
    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區(qū)域
    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;
    `

    // 動態(tài)生成適配類
    this.styleEl = document.createElement('style')

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

  // 頁面生命周期及瀏覽器大小監(jiān)聽
  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)
    })
  }

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

export default ScaleLayout
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市颁虐,隨后出現(xiàn)的幾起案子蛮原,更是在濱河造成了極大的恐慌,老刑警劉巖另绩,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件儒陨,死亡現(xiàn)場離奇詭異,居然都是意外死亡笋籽,警方通過查閱死者的電腦和手機(jī)蹦漠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來车海,“玉大人笛园,你說我怎么就攤上這事∈讨ィ” “怎么了研铆?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長州叠。 經(jīng)常有香客問我棵红,道長,這世上最難降的妖魔是什么留量? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任窄赋,我火速辦了婚禮哟冬,結(jié)果婚禮上楼熄,老公的妹妹穿的比我還像新娘忆绰。我一直安慰自己,他們只是感情好可岂,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布错敢。 她就那樣靜靜地躺著,像睡著了一般缕粹。 火紅的嫁衣襯著肌膚如雪稚茅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天平斩,我揣著相機(jī)與錄音亚享,去河邊找鬼。 笑死绘面,一個胖子當(dāng)著我的面吹牛欺税,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播揭璃,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼晚凿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瘦馍?” 一聲冷哼從身側(cè)響起歼秽,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎情组,沒想到半個月后燥筷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡院崇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年荆责,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片亚脆。...
    茶點(diǎn)故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡做院,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出濒持,到底是詐尸還是另有隱情键耕,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布柑营,位于F島的核電站屈雄,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏官套。R本人自食惡果不足惜酒奶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一蚁孔、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧惋嚎,春花似錦杠氢、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至摆尝,卻和暖如春温艇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背堕汞。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工勺爱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人讯检。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓琐鲁,卻偏偏與公主長得像,于是被迫代替她去往敵國和親视哑。 傳聞我的和親對象是個殘疾皇子绣否,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容