vue中实现拖拽排序功能

本文介绍了如何在Vue项目中使用vuedraggable库创建一个可编辑的列表,允许用户调整项目顺序,并通过`onMove`方法处理拖动事件。
摘要由CSDN通过智能技术生成

npm i vuedraggable

<template>
  <div class="app-container">
    <!-- <div :class="canEdit ? 'dargBtn-lock el-icon-unlock' : 'dargBtn-lock el-icon-lock'" @click="removeEvent()">
      {{ canEdit ? '调整' : '锁定' }}
    </div> -->
    <ul class="projset-content">
      <draggable
        style="display: flex; flex-direction: column"
        :forceFallback="true"
        :move="onMove"
        :list="imgList"
        handle=".dargBtn"
        :animation="1000"
        filter=".undraggable"
        fallbackClass="fallbackStyle"
        ghostClass="item_ghost"
        chosenClass="chosenStyle"
        dragClass="dragStyle"
      >
        <li v-for="(item, index) in imgList" :key="index" :class="canEdit ? 'draggable' : 'undraggable'">
          <div class="dargBtn">
            <svg-icon icon-class="drag" />
          </div>
          <img :src="item.path" alt="" />
          <span>{{ item.name }}</span>
        </li>
      </draggable>
    </ul>
  </div>
</template>

<script>
import Draggable from 'vuedraggable'
export default {
  components: { Draggable },
  data () {
    return {
      canEdit: true,
      imgList: [
        {
          path: 'https://lupic.cdn.bcebos.com/20210629/3000005161_14.jpg',
          name: '1'
        },
        {
          path: 'https://lupic.cdn.bcebos.com/20210629/26202931_14.jpg',
          name: '2'
        },
        {
          path: 'https://lupic.cdn.bcebos.com/20210629/27788166_14.jpg',
          name: '3'
        }
      ]
    }
  },
  created () {},
  mounted () {},
  methods: {
    onMove (relatedContext, draggedContext) {
      console.log(relatedContext.relatedContext.list)
    },
    removeEvent (item) {
      if (this.canEdit) {
        this.canEdit = false
      } else {
        this.canEdit = true
      }
      console.log(this.canEdit)
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  background: #ffffff;
  height: 100%;
  .dargBtn-lock {
    margin: 0px 50px;
    color: #2ea9df;
  }
  .dragStyle {
    padding: 10px;
    border-radius: 4px;
    opacity: 1;
  }
  .fallbackStyle {
    padding: 10px;
    border-radius: 4px;
    opacity: 1;
  }
  .chosenStyle {
    padding: 10px;
    border-radius: 4px;
    opacity: 1 !important;
  }
  .item_ghost {
    opacity: 0 !important;
  }
  .projset-content {
    list-style-type: none;
    position: relative;
    li {
      display: inline-block;
      margin: 10px;
    }
    img {
      width: 141px;
      height: 100px;
    }
    span {
      justify-content: center;
      display: flex;
    }
    .dargBtn {
      position: absolute;
      line-height: 100px;
      text-align: center;
      width: 141px;
      height: 100px;
      display: none;
      color: white;
      // background: rgba(101, 101, 101, 0.6);
    }
    .draggable {
      cursor: pointer;
      width: 141px;
      height: 100px;
    }
    .draggable:hover .dargBtn {
      display: block;
    }
  }
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值