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

vue和H5 draggable實(shí)現(xiàn)拖拽并替換效果

 更新時(shí)間:2020年07月29日 08:58:41   作者:小白菜大蘿卜  
這篇文章主要為大家詳細(xì)介紹了vue和H5 draggable實(shí)現(xiàn)拖拽并替換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

公司項(xiàng)目需要做拖拽替換效果,本人用的vue框架。在網(wǎng)上找了很多資料都是用的 Vue.Draggable(git地址)。但這個(gè)組件實(shí)現(xiàn)的拖拽后插入效果,我倒騰了很久也沒(méi)有替換效果(如果Vue.Draggable能實(shí)現(xiàn)拖拽替換效果的話請(qǐng)大神給我留言)。

JQ有實(shí)現(xiàn)拖拽的插件,我下載過(guò)一個(gè)插件并看過(guò)源碼,大致原理是給目標(biāo)元素設(shè)置定位屬性,通過(guò)監(jiān)聽(tīng)鼠標(biāo)mousedown,mouseup事件,再計(jì)算鼠標(biāo)位置變化,然后給元素樣式設(shè)置偏移值來(lái)實(shí)現(xiàn)拖拽效果的。

H5提供了專門的拖拽API 給元素添加 draggable 屬性,設(shè)置為 true就能實(shí)現(xiàn)拖拽了。本文使用的H5提供的拖拽API 以及vue 無(wú)其他任何添加,請(qǐng)放心使用

直接上代碼

<template>
 <div class="container">
  <div class="layout">
   <button
    class="layout-btn"
    @click="layoutType=val.value"
    v-for="val in layoutOptions"
    :key="val.value"
   >{{val.label}}</button>
  </div>

  <div
   class="group"
   :class="{'left-top-container': gindex===0,
    'right-top-container': gindex===1,
    'bottom-container': gindex===2,
    'top-container': gindex<2}"
   v-for="(group,gindex) in data"
   :key="gindex"
  >
   <div
    class="cls-default"
    v-for="(item,cindex) in group.children"
    :key="cindex"
    :data-id="gindex+'-'+cindex"
    draggable="true"
    @dragstart="onDragstart($event)"
    @dragend="onDragend($event)"
    @dragover="onDragover($event)"
    @drop="onDrop($event)"
    :style="{'background-color': item.color}"
    :class="{'cls1-0': cindex ===0 && layoutType==1,
    'cls2-0': (cindex ===0 || cindex ===1) && layoutType==2,
    'cls3-0': cindex ===0 && layoutType==3,
    'cls3-1': (cindex ===1 || cindex ===2) && layoutType==3,
    'cls4-0': cindex <4 && layoutType==4,
    'cls6-0': cindex === 0 && layoutType==6
    }"
   >
    <div class="content">{{item.color ? item.color : '我是空對(duì)象'}}</div>
   </div>
  </div>
  <div class="tips">上面兩個(gè)區(qū)域內(nèi)是展示區(qū)的內(nèi)容能互相拖拽
   <br>下面的是資源區(qū),只能復(fù)制出去覆蓋目標(biāo)區(qū)域,本身不會(huì)被替換掉
  </div>
 </div>
</template>

<script>
export default {
 data() {
  return {
   stargindex: "",
   endIndex: "",
   layoutType: "9",
   layoutOptions: [
    { label: "單分屏", value: 1 },
    { label: "二分屏", value: 2 },
    { label: "三分屏", value: 3 },
    { label: "四分屏", value: 4 },
    { label: "六分屏", value: 6 },
    { label: "九分屏", value: 9 }
   ],
   data: [
    {
     group: "left-show",
     title: "視頻播放區(qū)一",
     children: [
      {
       id: 6,
       color: "orange"
      },
      {
       id: 2,
       color: "yellow"
      },
      {},
      {},
      {},
      {},
      {
       id: 3,
       color: "cyan"
      },
      {},
      {
       id: 5,
       color: "brown"
      }
     ]
    },
    {
     group: "right-show",
     title: "視頻播放區(qū)二",
     children: [
      {},
      {
       id: 7,
       color: "pink"
      },
      {},
      {},
      { id: 4, color: "purple" },
      {},
      {},
      {},
      {
       id: 10,
       color: "gray"
      }
     ]
    },
    {
     group: "source",
     title: "視頻資源區(qū)",
     children: [
      {
       id: 11,
       color: "white"
      },
      {
       id: 12,
       color: "black"
      },
      {
       id: 13,
       color: "red"
      },
      {
       id: 14,
       color: "green"
      },
      {
       id: 15,
       color: "blue"
      }
     ]
    }
   ]
  };
 },
 methods: {
  onDragstart(event) {
   this.stargindex = event.target.getAttribute("data-id");
  },
  onDragend(event) {
   let startGroupIndex = this.stargindex.split("-")[0];
   let startChildIndex = this.stargindex.split("-")[1];
   let endGroupIndex = this.endIndex.split("-")[0];
   let endChildIndex = this.endIndex.split("-")[1];
   // 對(duì)數(shù)據(jù)做簡(jiǎn)單的深拷貝 目前不需要
   // let endObj = JSON.parse(
   //  JSON.stringify(this.data[endGroupIndex].children[endChildIndex])
   // );
   // let startObj = JSON.parse(
   //  JSON.stringify(this.data[startGroupIndex].children[startChildIndex])
   // );
   let endObj = this.data[endGroupIndex].children[endChildIndex];
   let startObj = this.data[startGroupIndex].children[startChildIndex];
   if (this.data[endGroupIndex].group === "source") {
    //往資源區(qū)拖拽時(shí) 不做任何替換操作
    return;
   }
   this.data[endGroupIndex].children.splice(endChildIndex, 1, startObj);
   if (this.data[startGroupIndex].group !== "source") {
    //拖拽起始區(qū)域不是 source時(shí) 把起始區(qū)域替換成拖拽后區(qū)域的數(shù)據(jù)
    this.data[startGroupIndex].children.splice(startChildIndex, 1, endObj);
   }
  },
  onDrop(event) {
   if (event.target.className.indexOf("cls-default") > -1) {
    this.endIndex = event.target.getAttribute("data-id");
   } else {
    this.endIndex = event.target.parentElement.getAttribute("data-id");
   }
  },
  onDragover(event) {
   event.preventDefault();
  }
 }
};
</script>

<style scoped>
.container {
 background-color: #eee;
 height: 800px;
}
.layout .layout-btn {
 background-color: #409eff;
 color: #fff;
 padding: 10px 15px;
 margin: 10px 15px;
}
.tips {
 font-size: 24px;
 text-align: center;
}
.group {
 float: left;
 overflow: hidden;
 box-sizing: border-box;
}
.group-title {
 height: 40px;
 line-height: 40px;
}
.cls-default {
 float: left;
 margin: 0;
 box-sizing: border-box;
 overflow: hidden;
 border: 1px solid #999;
}
.cls-default .content {
 text-align: center;
 padding-top: 20px;
 font-size: 20px;
}
.top-container {
 height: 400px;
 width: 40%;
 margin: 15px 5%;
}
.top-container .cls-default {
 width: 33.33%;
 height: 33.33%;
}
.top-container .cls1-0 {
 width: 100%;
 height: 100%;
}
.top-container .cls2-0 {
 width: 50%;
 height: 100%;
}
.top-container .cls3-0 {
 width: 50%;
 height: 100%;
}
.top-container .cls3-1 {
 width: 50%;
 height: 50%;
}
.top-container .cls4-0 {
 width: 50%;
 height: 50%;
}
.top-container .cls6-0 {
 width: 66.66%;
 height: 66.65%;
}
.bottom-container {
 width: 90%;
 height: 200px;
 margin: 15px 5%;
}
.bottom-container .cls-default {
 width: 15%;
 height: 150px;
}
</style>

寫(xiě)在最后

本文是我第一次寫(xiě)博客,寫(xiě)的比較隨意,樣式處理也是很隨心。如有錯(cuò)誤請(qǐng)指正。

后面有時(shí)間會(huì)完善組件的功能。參考Vue.Draggable(git地址)這個(gè)組件。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue中的reactive函數(shù)操作代碼

    Vue中的reactive函數(shù)操作代碼

    這篇文章主要介紹了Vue中的reactive函數(shù),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • element-ui upload組件多文件上傳的示例代碼

    element-ui upload組件多文件上傳的示例代碼

    這篇文章主要介紹了element-ui upload組件多文件上傳的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • 淺析Vue中生命周期函數(shù)的區(qū)別

    淺析Vue中生命周期函數(shù)的區(qū)別

    生命周期分為四個(gè)對(duì)子,根據(jù)不同的情況使用不同的函數(shù),這篇文章主要為大家介紹了這些函數(shù)的使用與區(qū)別,感興趣的小伙伴可以了解一下
    2023-08-08
  • vue+Echart實(shí)現(xiàn)立體柱狀圖

    vue+Echart實(shí)現(xiàn)立體柱狀圖

    這篇文章主要為大家詳細(xì)介紹了vue+Echart實(shí)現(xiàn)立體柱狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue中ElementUI結(jié)合transform使用時(shí)如何修復(fù)el-select彈框定位不準(zhǔn)確問(wèn)題

    Vue中ElementUI結(jié)合transform使用時(shí)如何修復(fù)el-select彈框定位不準(zhǔn)確問(wèn)題

    這篇文章主要介紹了Vue中ElementUI結(jié)合transform使用時(shí)如何修復(fù)el-select彈框定位不準(zhǔn)確問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • 詳解Vuex的屬性

    詳解Vuex的屬性

    Vuex是專為Vue.js應(yīng)用程序開(kāi)發(fā)的狀態(tài)管理模式,這篇文章主要介紹了Vuex的屬性,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • 基于Vue實(shí)現(xiàn)我的錢包充值功能的示例代碼

    基于Vue實(shí)現(xiàn)我的錢包充值功能的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何基于Vue實(shí)現(xiàn)我的錢包充值功能,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下
    2024-01-01
  • Vue淺析講解動(dòng)態(tài)組件與緩存組件及異步組件的使用

    Vue淺析講解動(dòng)態(tài)組件與緩存組件及異步組件的使用

    這篇文章主要介紹了Vue開(kāi)發(fā)中的動(dòng)態(tài)組件與緩存組件及異步組件的使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • vue 中的 v-model詳解

    vue 中的 v-model詳解

    文章介紹了Vue中的v-model實(shí)現(xiàn)原理,包括數(shù)據(jù)監(jiān)聽(tīng)和UI通知,通過(guò)綁定props.modelValue和使用update:modelValue,子組件可以實(shí)現(xiàn)數(shù)據(jù)變更通知父組件,Vue3提供了defineModel來(lái)簡(jiǎn)化v-model的定義,并支持自定義v-model變量名,感興趣的朋友一起看看吧
    2025-01-01
  • vue的@change的用法及操作代碼

    vue的@change的用法及操作代碼

    @change 是 Vue.js 中用于監(jiān)聽(tīng)表單元素值變化的事件處理器,這篇文章主要介紹了vue的@change的用法,需要的朋友可以參考下
    2023-10-10

最新評(píng)論

岳西县| 孝义市| 三门县| 扶沟县| 临夏县| 广灵县| 阿城市| 佛冈县| 新竹市| 包头市| 香格里拉县| 鹤庆县| 聂荣县| 会东县| 浏阳市| 平和县| 靖边县| 白河县| 黑水县| 泾川县| 布尔津县| 特克斯县| 自治县| 聂荣县| 兴和县| 固镇县| 彭水| 原阳县| 武功县| 深泽县| 平昌县| 栾城县| 婺源县| 渭源县| 五常市| 武功县| 林芝县| 东城区| 弋阳县| 库车县| 腾冲县|