【Vue实用功能】Vue实现拖拽元素、列表拖拽排序

Vue实现拖拽元素、列表拖拽排序

需求:
	1、左右两个容器,左边和右边的元素可以拖动互换
	2、列表拖拽排序

组件使用

Vue.Draggable是一款基于Sortable.js实现的vue拖拽插件支持移动设备、拖拽和选择文本、智能滚动,可以在不同列表间拖拽、不依赖jQuery为基础,vue2过渡动画兼容、支持撤销操作,总之是一款非常优秀的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" 设置拖动元素的占位符类名,你的自定义样式可能需要加!important才能生效,并把forceFallback属性设置成true
          chosenClass :ghostClass="hostClass" 被选中目标的样式,你的自定义样式可能需要加!important才能生效,并把forceFallback属性设置成true
          filter :filter=".unmover" 设置了unmover样式的元素不允许拖动
          -->
        <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>
  //导入draggable组件
  import draggable from 'vuedraggable'
  export default {
    //注册draggable组件
    components: {
      draggable,
    },
    data() {
      return {
        //定义要被拖拽对象的数组
        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回调方法
      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>

在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值