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

Vue實(shí)用功能之實(shí)現(xiàn)拖拽元素、列表拖拽排序

 更新時(shí)間:2022年10月28日 10:37:01   作者:周圍都是小趴菜  
在日常開發(fā)中,特別是管理端,經(jīng)常會(huì)遇到要實(shí)現(xiàn)拖拽排序的效果,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)用功能之實(shí)現(xiàn)拖拽元素、列表拖拽排序的相關(guān)資料,需要的朋友可以參考下

Vue實(shí)現(xiàn)拖拽元素、列表拖拽排序

需求:
    1、左右兩個(gè)容器,左邊和右邊的元素可以拖動(dòng)互換
    2、列表拖拽排序

組件使用

Vue.Draggable是一款基于Sortable.js實(shí)現(xiàn)的vue拖拽插件支持移動(dòng)設(shè)備、拖拽和選擇文本、智能滾動(dòng),可以在不同列表間拖拽、不依賴jQuery為基礎(chǔ),vue2過渡動(dòng)畫兼容、支持撤銷操作,總之是一款非常優(yōu)秀的vue拖拽組件

npm或yarn安裝方式

yarn add vuedraggable
npm i -S vuedraggable
<template>
  <div>
    <!--使用draggable組件-->
    <div class="itxst">
      <div>自定義控制拖拽和停靠</div>
      <div class="col">
        <!-- 左邊容器 -->
        <!-- 
          group: :group= "name",相同的組之間可以相互拖拽或者 { name: "...", pull: [true, false, 'clone', array , function], put: [true, false, array , function] }
          ghostClass::ghostClass="ghostClass" 設(shè)置拖動(dòng)元素的占位符類名,你的自定義樣式可能需要加!important才能生效,并把forceFallback屬性設(shè)置成true
          chosenClass :ghostClass="hostClass" 被選中目標(biāo)的樣式,你的自定義樣式可能需要加!important才能生效,并把forceFallback屬性設(shè)置成true
          filter :filter=".unmover" 設(shè)置了unmover樣式的元素不允許拖動(dòng)
          -->
        <draggable v-model="arr1" group="itxst" ghostClass="ghost" chosenClass="chosen" filter=".forbid" animation="300"
          :move="onMove">
          <transition-group>
            <div :class="item.id==1?'item forbid':'item'" v-for="item in arr1" :key="item.id">{{item.name}}</div>
          </transition-group>
        </draggable>
      </div>
      <!-- 右邊容器 -->
      <div class="col">
        <draggable v-model="arr2" group="itxst" ghostClass="ghost" chosenClass="chosen" filter=".forbid" animation="300"
          :move="onMove">
          <transition-group>
            <div :class="item.id==1?'item forbid':'item'" v-for="item in arr2" :key="item.id">{{item.name}}</div>
          </transition-group>
        </draggable>
      </div>
    </div>
  </div>
</template>
<script>
  //導(dǎo)入draggable組件
  import draggable from 'vuedraggable'
  export default {
    //注冊(cè)draggable組件
    components: {
      draggable,
    },
    data() {
      return {
        //定義要被拖拽對(duì)象的數(shù)組
        arr1: [{
            id: 1,
            name: 'www.itxst.com(不允許??浚?
          },
          {
            id: 2,
            name: 'www.jd.com'
          },
          {
            id: 3,
            name: 'www.baidu.com'
          },
          {
            id: 5,
            name: 'www.google.com'
          },
          {
            id: 4,
            name: 'www.taobao.com(不允許拖拽)'
          }
        ],
        arr2: [{
            id: 11,
            name: '微軟'
          },
          {
            id: 12,
            name: '亞馬遜'
          },
          {
            id: 13,
            name: '京東'
          },
          {
            id: 15,
            name: '谷歌'
          },
          {
            id: 14,
            name: '蘋果'
          }
        ]
      };
    },
    methods: {
      //move回調(diào)方法
      onMove(e, originalEvent) {
        console.log(e)
        console.log(originalEvent)
        //不允許???
        if (e.relatedContext.element.id == 1) return false;
        //不允許拖拽
        if (e.draggedContext.element.id == 4) return false;
        return true;
      },
    },
  };
</script>
<style scoped>
  /*定義要拖拽元素的樣式*/
  .drag {
    background-color: blue !important;
    border: solid 3px red;
  }

  .chosen {
    background-color: #333 !important;
    color: #fff;
  }

  .ghost {
    background-color: red !important;
  }

  .itxst {
    margin: 10px;
    text-align: left;
  }

  .col {
    width: 40%;
    flex: 1;
    padding: 10px;
    border: solid 1px #eee;
    border-radius: 5px;
    float: left;
  }

  .col+.col {
    margin-left: 10px;
  }

  .item {
    padding: 6px 12px;
    margin: 0px 10px 0px 10px;
    border: solid 1px #eee;
    background-color: #f1f1f1;
    text-align: left;
  }

  .item+.item {
    border-top: none;
    margin-top: 6px;
  }

  .item:hover {
    background-color: #fdfdfd;
    cursor: move;
  }
</style>

補(bǔ)充:排序動(dòng)畫

如果不熟悉Vue的transition-group,請(qǐng)先學(xué)習(xí)Vue的列表的排序過渡,這里不再贅述。

為了便于和上面的代碼進(jìn)行比較,同樣一次性把全部代碼貼出,可以看到代碼變動(dòng)并不大,只需把HTML的ul元素改為transition-group,在methods中新增shuffle方法,在CSS中新增一個(gè)過渡transition: transform .3s;即可實(shí)現(xiàn)開頭第一張圖所展示的拖拽排序效果了。

<template>
? <div>
? ? <transition-group
? ? ? name="drag"
? ? ? class="list"
? ? ? tag="ul"
? ? >
? ? ? <li
? ? ? ? @dragenter="dragenter($event, index)"
? ? ? ? @dragover="dragover($event, index)"
? ? ? ? @dragstart="dragstart(index)"
? ? ? ? draggable
? ? ? ? v-for="(item, index) in list"
? ? ? ? :key="item.label"
? ? ? ? class="list-item">
? ? ? ? {{item.label}}
? ? ? </li>
? ? </transition-group>
? </div>
</template>
<script>
export default {
? data() {
? ? return {
? ? ? list: [
? ? ? ? { label: '列表1' },
? ? ? ? { label: '列表2' },
? ? ? ? { label: '列表3' },
? ? ? ? { label: '列表4' },
? ? ? ? { label: '列表5' },
? ? ? ? { label: '列表6' },
? ? ? ],
? ? ? dragIndex: '',
? ? ? enterIndex: '',
? ? };
? },
? methods: {
? ? shuffle() {
? ? ? this.list = this.$shuffle(this.list);
? ? },
? ? dragstart(index) {
? ? ? this.dragIndex = index;
? ? },
? ? dragenter(e, index) {
? ? ? e.preventDefault();
? ? ? // 避免源對(duì)象觸發(fā)自身的dragenter事件
? ? ? if (this.dragIndex !== index) {
? ? ? ? const moving = this.list[this.dragIndex];
? ? ? ? this.list.splice(this.dragIndex, 1);
? ? ? ? this.list.splice(index, 0, moving);
? ? ? ? // 排序變化后目標(biāo)對(duì)象的索引變成源對(duì)象的索引
? ? ? ? this.dragIndex = index;
? ? ? }
? ? },
? ? dragover(e, index) {
? ? ? e.preventDefault();
? ? },
? },
};
</script>
<style lang="scss" scoped>
.list {
? list-style: none;
? .drag-move {
? ? transition: transform .3s;
? }
? .list-item {
? ? cursor: move;
? ? width: 300px;
? ? background: #EA6E59;
? ? border-radius: 4px;
? ? color: #FFF;
? ? margin-bottom: 6px;
? ? height: 50px;
? ? line-height: 50px;
? ? text-align: center;
? }
}
</style>

總結(jié)

到此這篇關(guān)于Vue實(shí)用功能之實(shí)現(xiàn)拖拽元素、列表拖拽排序的文章就介紹到這了,更多相關(guān)Vue列表拖拽排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

察雅县| 都昌县| 柳州市| 玛纳斯县| 江达县| 饶阳县| 阿坝县| 佛冈县| 济阳县| 嘉定区| 武山县| 望江县| 阳春市| 宝兴县| 忻州市| 临洮县| 章丘市| 准格尔旗| 武威市| 阿拉尔市| 亳州市| 孙吴县| 金湖县| 剑河县| 都安| 阳曲县| 新乡市| 合川市| 佳木斯市| 玉林市| 和政县| 东光县| 锡林郭勒盟| 时尚| 阜新市| 鹤岗市| 山阴县| 天长市| 二手房| 林芝县| 汝南县|