VUE實現Studio管理後臺(一):鼠標拖放改變窗口大小

近期改版RXEditor,把改版過程,用到的技術點,記錄下來。昨天完成了靜態頁面的製作,製作過程並未詳細記錄,後期已經不願再補了,有些遺憾。不過工作成果完整保留在github上,地址:https://github.com/vularsoft/studio-ui

這個項目下面的html-demo.html,便是靜態文件。

 

話不多說,今天就把昨天的HTML轉化成VUE。

先看效果:

佈局原理

頁面採用純彈性盒子flex box佈局,無float postion等,頁面分成如下幾個區域:

細實線描述的是各個DIV之間的嵌套關係,粗黑線是獨立的DIV,我稱它們爲把手(HADLE),主要用來接受鼠標拖動事件,以完成拖動操作。handle非爲兩種,橫向x-handle,縱向y-handle,css中定義x-handle寬度爲3px,高度爲100%,y-handle高度爲3px,寬度爲100%,鼠標光標也相應設置一下:

 

.vular-studio .x-handle{
  width: 3px;
  cursor: w-resize;
  z-index: 10;
}

.vular-studio .y-handle{
  height: 3px;
  cursor: s-resize;
  z-index: 10;
}

拖動原理
帶有把手的區域固定大小(固定寬度或者高度),不帶把手的部分跟隨彈性盒子變化。把手handle是一個獨立的VUE組件,它把拖動信息傳遞給父窗口,父窗口改變自身大小。以bottom-area爲例,這是一個可以改變自身大小的DIV:

 

<template>
  <div class="bottom-area" :style="{height:height + 'px'}">
    <YHandle @heightChange="heightChange"></YHandle>
    <div class="bottom-inner">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import YHandle from './YHandle.vue'
export default {
  name: 'BottomArea',
  components:{
    YHandle,
  },
  data () {
    return {
      height:220,
    }
  },
  methods: {
    heightChange(movement){
      this.height += movement
      if(this.height < 30){
        this.height = 30
      }
      if(this.height > 600){
        this.height = 600
      }
    },
  },
}
</script>

它對應的把手代碼:

<template>
  <div class="y-handle" 
    @mousedown="mouseDown" 
  ></div>
</template>

<script>
export default {
  name: 'YHandle',
  data () {
    return {
      lastY:''
    }
  },

  created () {
    document.addEventListener('mouseup', this.mouseUp)
  },

  destroyed() {
    document.removeEventListener('mouseup', this.mouseUp)
  },


  methods: {
    mouseDown(event){
      document.addEventListener('mousemove', this.mouseMove)
      this.lastY = event.screenY
    },
    mouseMove(event){
      console.log('move')
      this.$emit('heightChange', this.lastY - event.screenY)
      this.lastY = event.screenY
    },
    mouseUp(event){
      this.lastY = ''
      document.removeEventListener('mousemove', this.mouseMove)
    },
  },
}
</script>

製作步驟

先建一個VUE項目:
1、安裝node
2、安裝webpack
3、安裝VUE
4、新建VUE項目:vue init webpack-simple
5、根據相應佈局製作VUE組件
具體代碼,請參考:https://github.com/vularsoft/studio-ui,根據標註,獲取相應的版本記錄即可。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章