欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue模塊移動組件的實現(xiàn)示例

 更新時間:2020年05月20日 09:58:30   作者:懵鐘小粉紅  
這篇文章主要介紹了vue模塊移動組件的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一直都想實現(xiàn)類似于五百丁中簡歷填寫中模塊跟隨鼠標移動的組件,最近閑來無事,自己琢磨實現(xiàn)了一個差不多的組件。

其中每個模塊都是組件調(diào)入(項目經(jīng)驗、教育經(jīng)驗、工作經(jīng)驗等),所以這里也用到了動態(tài)加載組件方式。

思路:鼠標移入模塊,顯示相應模塊的點擊移動按鈕,點擊A模塊移動按鈕,此時原先A模塊所在的位置上變?yōu)橥蟿拥竭@里綠框模塊,同時鼠標下懸浮著A模塊,鼠標移動,懸浮的A模塊跟隨移動,綠框也跟隨上下移動。

父組件

1、父組件template中的代碼

<div class="component-box" ref="compBox">
 <component
   v-for="(item, index) in comRoute"
   :is="item"
   :key="index"
   @getData="getData">
</component>
 <div :class="['translate-box', {'move-icon':transType}]"
    ref="translateBox" v-if="transType">
  <component :is="transCom"></component>
 </div>
</div>

2、data中定義的屬性

comList: ['educationExp', 'workExp', 'projectExp'], // 模塊列表
comLen: 0, // 模塊的長度
comType: '', // 當前移動的模塊
transType: '', // 當前移動的模塊
coordinate: { // 鼠標坐標
 mouseX: 0,
 mouseY: 0,
},
downFlag: false, // 當前是否點擊模塊移動
mouseYBefore: 0, // 記錄鼠標點擊時Y坐標以及鼠標每移動30后重新記錄鼠標Y坐標
mouseYLast: 0, // 實時記錄鼠標移動時的Y坐標
nowCom: '', // 移動模塊時,上一個模塊或者下一個模塊的名稱
forFlage: false, // forEach遍歷結(jié)束的標識
comRoute: [], // 動態(tài)加載組件列表
transCom: null, // 點擊后懸浮的組件
compBox: null, // 獲取當前組件在頁面中的位置信息

3、scrollTop為頁面滾動的距頂部的距離,從父組件傳過來

props: { scrollTop: Number,}

4、watch一些屬性

watch: {
 comList: { 
  handler(val) {
   this.getCom(val); // 模塊列表改變時,實時加載組件
  },
  deep: true,
  immediate: true, // 聲明了之后會立馬執(zhí)行handler里面的函數(shù)
 },
 transType(val) { // 懸浮模塊加載組件
  if (val) {
   this.transCom = () => import(`./default/${val}`);
  }
 },
 scrollTop: { // 監(jiān)聽頁面滾動
  handler() {},
  immediate: true,
 },
 comType(newVal) {
  if (newVal) {
   this.comList.forEach((item, index) => {
    if (item === newVal) {
     this.comList[index] = 'moveBox'; // 將組建列表中為comType的元素改為moveBox組件
    }
   });
   this.getCom(this.comList);
  }
 },
 downFlag(newVal) { // 鼠標已經(jīng)點擊
  const nowThis = this;
  document.onmousemove = function (e) {
   if (newVal) { // 鼠標移動賦值
    nowThis.coordinate.mouseX = e.clientX;
    nowThis.coordinate.mouseY = e.clientY;
   }
  };
  document.onmouseup = function () { // 鼠標松開
   document.onmousemove = null;
   if (newVal) {
    nowThis.transType = ''; // 懸浮組件置空
    nowThis.comList.forEach((item, index) => {
     if (item === 'moveBox') { // 尋找moveBox所在位置
      nowThis.comList[index] = nowThis.comType; // 還原成點擊組件
     }
    });
    nowThis.downFlag = false;
    nowThis.comType = '';
    nowThis.getCom(nowThis.comList);
   }
  };
 },
 coordinate: {
  handler(newVal) { // 懸浮組件位置
   this.$refs.translateBox.style.top = `${newVal.mouseY + this.scrollTop - 40 - this.compBox.y}px`;
   this.$refs.translateBox.style.left = `${newVal.mouseX - this.compBox.x - 200}px`;
   this.mouseYLast = newVal.mouseY;
  },
  deep: true,
 },
 mouseYLast(newVal) { // 鼠標移動Y坐標
  this.forFlage = false; 
  if (newVal - this.mouseYBefore > 30) {  // 移動30鼠標向下移,每移動30,moveBox移動一次
   this.comList.forEach((item, index) => {
    if (item === 'moveBox' && index < this.comLen - 1 && !this.forFlage) {
     this.nowCom = this.comList[index + 1];
     this.$set(this.comList, index + 1, 'moveBox');
     this.$set(this.comList, index, this.nowCom);
     this.mouseYBefore = newVal;
     this.forFlage = true;
    }
   });
  } else if (newVal - this.mouseYBefore < -30) {   // 鼠標向上移
   this.comList.forEach((item, index) => {
    if (item === 'moveBox' && index > 0 && !this.forFlage) {
     this.nowCom = this.comList[index - 1];
     // this.comList[index - 1] = 'moveBox';
     // this.comList[index] = this.nowCom;
     // this.comList[index]數(shù)組中采用這種方式賦值,vue是不能檢測到數(shù)組的變動的
     this.$set(this.comList, index - 1, 'moveBox');
     this.$set(this.comList, index, this.nowCom);
     this.mouseYBefore = newVal;
     this.forFlage = true;
    }
   });
  }
 },
},

其中 forFlage的作用是,在forEach中不能使用break這樣來結(jié)束循環(huán),所以用forFlage來表示,當遍歷到moveBox后, 就結(jié)束遍歷

5、methods

methods: {
 getCom(val) {
  this.comRoute = [];
  val.forEach((item) => {
   this.comRoute.push(() => import(`./default/${item}`));
  });
 },
 getData(data, dataY, dataX) { // 模塊組件點擊后,父組件中調(diào)用此方法,并傳值過來
  this.comType = data;
  this.transType = data;  // 目前考慮點擊后立即移動,點擊不移動的情況后期再考慮
  this.downFlag = true;
  this.mouseYBefore = dataY;
  this.$nextTick(() => {
   this.$refs.translateBox.style.top = `${dataY + this.scrollTop - 40 - this.compBox.y}px`;
   this.$refs.translateBox.style.left = `${dataX - this.compBox.x - 200}px`;
  });
 },
},

6、mounted

mounted() {
 this.comLen = this.comList.length;
 this.compBox = this.$refs.compBox.getBoundingClientRect();
 const that = this;
 window.onresize = () => (() => {
  that.compBox = this.$refs.compBox.getBoundingClientRect();
 })();
},

子組件

1、子組件template代碼

<div class="pad-box hover-box name-box">
 <p class="absolute-box">
  <i class="el-icon-rank operation-icon move-icon"    @mousedown="mouseDown"></i>
  <i class="el-icon-circle-plus operation-icon"></i>
  <i class="el-icon-s-tools operation-icon"></i>
 </p>
 教育經(jīng)驗
</div>

2、script

export default {
 name: 'educationExp',
 data() {
  return {
   comType: 'educationExp',
   mouseYBefore: 0,
   mouseXBefore: 0,
  };
 },
 methods: {
  mouseDown(e) {
   this.mouseYBefore = e.clientY;
   this.mouseXBefore = e.clientX;
   this.$emit('getData', this.comType, this.mouseYBefore, this.mouseXBefore);
  },
 },
};

到此這篇關(guān)于vue模塊移動組件的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue模塊移動組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue-cli3.0項目打包后如何修改訪問后端地址

    vue-cli3.0項目打包后如何修改訪問后端地址

    這篇文章主要介紹了vue-cli3.0項目打包后如何修改訪問后端地址,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • vue實現(xiàn)動態(tài)表單動態(tài)渲染組件的方式(1)

    vue實現(xiàn)動態(tài)表單動態(tài)渲染組件的方式(1)

    這篇文章主要為大家詳細介紹了vue實現(xiàn)動態(tài)表單動態(tài)渲染組件的方式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue-socket.io接收不到數(shù)據(jù)問題的解決方法

    vue-socket.io接收不到數(shù)據(jù)問題的解決方法

    這篇文章主要介紹了解決vue-socket.io接收不到數(shù)據(jù)問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • vue跳轉(zhuǎn)頁面常用的幾種方法匯總

    vue跳轉(zhuǎn)頁面常用的幾種方法匯總

    這篇文章主要介紹了vue跳轉(zhuǎn)頁面常用的幾種方法匯總,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Vue性能優(yōu)化的方法

    Vue性能優(yōu)化的方法

    這篇文章主要介紹了Vue性能優(yōu)化的方法,文中講解非常細致,幫助大家更好的理解和學習vue,感興趣的朋友可以了解下
    2020-07-07
  • vue+echarts實現(xiàn)條紋柱狀橫向圖

    vue+echarts實現(xiàn)條紋柱狀橫向圖

    這篇文章主要為大家詳細介紹了vue+echarts實現(xiàn)條紋柱狀橫向圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue前端獲取/切換麥克風、播放采集音頻和采集音量大小完整代碼

    vue前端獲取/切換麥克風、播放采集音頻和采集音量大小完整代碼

    這篇文章主要給大家介紹了關(guān)于vue前端獲取/切換麥克風、播放采集音頻和采集音量大小的相關(guān)資料,文中通過圖文以及代碼介紹的非常詳細,對大家學習或者使用vue具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-12-12
  • vue-router路由模式詳解(小結(jié))

    vue-router路由模式詳解(小結(jié))

    這篇文章主要介紹了vue-router路由模式詳解(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Vue3中Suspense異步加載組件的問題

    Vue3中Suspense異步加載組件的問題

    在我們?nèi)粘i_發(fā)中,有些組件里面加載非常慢,導致我們路由跳轉(zhuǎn)的時候回出現(xiàn)卡頓情況,這篇文章主要介紹了Vue3:?Suspense異步加載組件,需要的朋友可以參考下
    2023-12-12
  • vue項目中如何實現(xiàn)網(wǎng)頁的截圖功能?(html2canvas)

    vue項目中如何實現(xiàn)網(wǎng)頁的截圖功能?(html2canvas)

    這篇文章主要介紹了vue項目中如何實現(xiàn)網(wǎng)頁的截圖功能?(html2canvas),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評論