最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue模塊移動(dòng)組件的實(shí)現(xiàn)示例

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

一直都想實(shí)現(xiàn)類似于五百丁中簡(jiǎn)歷填寫(xiě)中模塊跟隨鼠標(biāo)移動(dòng)的組件,最近閑來(lái)無(wú)事,自己琢磨實(shí)現(xiàn)了一個(gè)差不多的組件。

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

思路:鼠標(biāo)移入模塊,顯示相應(yīng)模塊的點(diǎn)擊移動(dòng)按鈕,點(diǎn)擊A模塊移動(dòng)按鈕,此時(shí)原先A模塊所在的位置上變?yōu)橥蟿?dòng)到這里綠框模塊,同時(shí)鼠標(biāo)下懸浮著A模塊,鼠標(biāo)移動(dòng),懸浮的A模塊跟隨移動(dòng),綠框也跟隨上下移動(dòng)。

父組件

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, // 模塊的長(zhǎng)度
comType: '', // 當(dāng)前移動(dòng)的模塊
transType: '', // 當(dāng)前移動(dòng)的模塊
coordinate: { // 鼠標(biāo)坐標(biāo)
 mouseX: 0,
 mouseY: 0,
},
downFlag: false, // 當(dāng)前是否點(diǎn)擊模塊移動(dòng)
mouseYBefore: 0, // 記錄鼠標(biāo)點(diǎn)擊時(shí)Y坐標(biāo)以及鼠標(biāo)每移動(dòng)30后重新記錄鼠標(biāo)Y坐標(biāo)
mouseYLast: 0, // 實(shí)時(shí)記錄鼠標(biāo)移動(dòng)時(shí)的Y坐標(biāo)
nowCom: '', // 移動(dòng)模塊時(shí),上一個(gè)模塊或者下一個(gè)模塊的名稱
forFlage: false, // forEach遍歷結(jié)束的標(biāo)識(shí)
comRoute: [], // 動(dòng)態(tài)加載組件列表
transCom: null, // 點(diǎn)擊后懸浮的組件
compBox: null, // 獲取當(dāng)前組件在頁(yè)面中的位置信息

3、scrollTop為頁(yè)面滾動(dòng)的距頂部的距離,從父組件傳過(guò)來(lái)

props: { scrollTop: Number,}

4、watch一些屬性

watch: {
 comList: { 
  handler(val) {
   this.getCom(val); // 模塊列表改變時(shí),實(shí)時(shí)加載組件
  },
  deep: true,
  immediate: true, // 聲明了之后會(huì)立馬執(zhí)行handler里面的函數(shù)
 },
 transType(val) { // 懸浮模塊加載組件
  if (val) {
   this.transCom = () => import(`./default/${val}`);
  }
 },
 scrollTop: { // 監(jiān)聽(tīng)頁(yè)面滾動(dòng)
  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) { // 鼠標(biāo)已經(jīng)點(diǎn)擊
  const nowThis = this;
  document.onmousemove = function (e) {
   if (newVal) { // 鼠標(biāo)移動(dòng)賦值
    nowThis.coordinate.mouseX = e.clientX;
    nowThis.coordinate.mouseY = e.clientY;
   }
  };
  document.onmouseup = function () { // 鼠標(biāo)松開(kāi)
   document.onmousemove = null;
   if (newVal) {
    nowThis.transType = ''; // 懸浮組件置空
    nowThis.comList.forEach((item, index) => {
     if (item === 'moveBox') { // 尋找moveBox所在位置
      nowThis.comList[index] = nowThis.comType; // 還原成點(diǎn)擊組件
     }
    });
    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) { // 鼠標(biāo)移動(dòng)Y坐標(biāo)
  this.forFlage = false; 
  if (newVal - this.mouseYBefore > 30) {  // 移動(dòng)30鼠標(biāo)向下移,每移動(dòng)30,moveBox移動(dòng)一次
   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) {   // 鼠標(biāo)向上移
   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是不能檢測(cè)到數(shù)組的變動(dòng)的
     this.$set(this.comList, index - 1, 'moveBox');
     this.$set(this.comList, index, this.nowCom);
     this.mouseYBefore = newVal;
     this.forFlage = true;
    }
   });
  }
 },
},

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

5、methods

methods: {
 getCom(val) {
  this.comRoute = [];
  val.forEach((item) => {
   this.comRoute.push(() => import(`./default/${item}`));
  });
 },
 getData(data, dataY, dataX) { // 模塊組件點(diǎn)擊后,父組件中調(diào)用此方法,并傳值過(guò)來(lái)
  this.comType = data;
  this.transType = data;  // 目前考慮點(diǎn)擊后立即移動(dòng),點(diǎn)擊不移動(dòng)的情況后期再考慮
  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)驗(yàn)
</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模塊移動(dòng)組件的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue模塊移動(dòng)組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue.js 圖標(biāo)選擇組件實(shí)踐詳解

    Vue.js 圖標(biāo)選擇組件實(shí)踐詳解

    這篇文章主要介紹了Vue.js 圖標(biāo)選擇組件實(shí)踐詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Vue打包上線之后部分CSS不生效問(wèn)題的解決辦法

    Vue打包上線之后部分CSS不生效問(wèn)題的解決辦法

    在vue項(xiàng)目中開(kāi)發(fā)環(huán)境的樣式?jīng)]問(wèn)題,但是打包上線后,樣式不生效,下面這篇文章主要給大家介紹了關(guān)于Vue打包上線之后部分CSS不生效問(wèn)題的解決辦法,需要的朋友可以參考下
    2022-12-12
  • vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個(gè)下載按鈕(操作方法)

    vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個(gè)下載按鈕(操作方法)

    這篇文章主要介紹了vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個(gè)下載按鈕,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • vue?下載文檔亂碼的解決

    vue?下載文檔亂碼的解決

    這篇文章主要介紹了vue?下載文檔亂碼的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue transition過(guò)渡組件詳解

    Vue transition過(guò)渡組件詳解

    我們現(xiàn)在可以了解一下vue的過(guò)渡,vue在插入、更新以及移除DOM元素的時(shí)候,提供了很多不同方式過(guò)渡的效果,如果在css過(guò)渡自動(dòng)應(yīng)用class,在過(guò)渡鉤子函數(shù)中使用JavaScript直接操作DOM就可以了
    2022-08-08
  • vue3 elementPlus 表格實(shí)現(xiàn)行列拖拽及列檢索功能(完整代碼)

    vue3 elementPlus 表格實(shí)現(xiàn)行列拖拽及列檢索功能(完整代碼)

    本文通過(guò)實(shí)例代碼給大家介紹vue3 elementPlus 表格實(shí)現(xiàn)行列拖拽及列檢索功能,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-10-10
  • Vue3中使用pinia的示例代碼

    Vue3中使用pinia的示例代碼

    這篇文章主要介紹了Vue3中使用pinia,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • Vue項(xiàng)目?jī)?yōu)化打包詳解

    Vue項(xiàng)目?jī)?yōu)化打包詳解

    這篇文章主要為大家介紹了Vue項(xiàng)目的優(yōu)化打包,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • Vue源碼之rollup環(huán)境搭建步驟詳解

    Vue源碼之rollup環(huán)境搭建步驟詳解

    這篇文章主要介紹了Vue源碼之rollup環(huán)境搭建步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • vue中接口域名配置為全局變量的實(shí)現(xiàn)方法

    vue中接口域名配置為全局變量的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇vue中接口域名配置為全局變量的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09

最新評(píng)論

鹤山市| 额尔古纳市| 平利县| 吉首市| 错那县| 内黄县| 星座| 丰原市| 天峨县| 马公市| 红安县| 石楼县| 阜康市| 博爱县| 莎车县| 普宁市| 彝良县| 沂源县| 历史| 信阳市| 手机| 曲周县| 鹤岗市| 乐东| 民勤县| 利辛县| 新绛县| 柏乡县| 阿克| 淳化县| 灌阳县| 镇赉县| 和政县| 辉县市| 启东市| 丰宁| 化州市| 家居| 海南省| 兴海县| 兖州市|