Vue2拖拽插件(vuedraggable)

简介: 这篇文章介绍了如何在Vue 3框架中使用`vuedraggable`插件来实现拖拽功能,并提供了插件的安装、配置和事件处理的示例。

参考文档: vue.draggable disabled 开启和禁用拖动 - itxst.com

效果如下图:

① 安装插件:yarn add vuedraggable

②引入插件并使用:

<template>
  <div class="draggable">
    <draggable
      v-model="dragData"
      group="player"
      animation="500"
      delay="0"
      :disabled="false"
      ghostClass="ghost"
      chosenClass="chosen"
      forceFallback
      @start="onStart"
      @end="onEnd"
      :move="onMove">
      <div class="u-player" v-for="(data, index) in dragData" :key="index">{
  
  { data.name || '--'}} : {
  
  { data.num }}</div>
    </draggable>
  </div>
</template>
<script>
import draggable from 'vuedraggable'
export default {
  name: 'Draggable',
  components: {
    draggable
  },
  data () {
    return {
      dragData: [
        {
          name: 'curry',
          num: 30
        },
        {
          name: 'klay',
          num: 11
        },
        {
          name: 'kobe',
          num: 24
        },
        {
          name: 'james',
          num: 23
        },      
        {
          name: 'leo',
          num: 36
        }
      ]
    }
  },
  methods: {
    onStart (e) { // 开始拖动时触发的事件
      console.log('start:', e)
      console.log('拖拽操作前的索引oldIndex:', e.oldIndex)
      console.log('拖拽完成后的索引newIndex:', e.newIndex)
    },
    onEnd (e) { // 拖动完成时触发的事件
      console.log('end:', e)
      console.log('拖拽操作前的索引oldIndex:', e.oldIndex)
      console.log('拖拽完成后的索引newIndex:', e.newIndex)
    },
    onMove (e, originalEvent) { // 拖拽move事件回调
      console.log('move:', e)
      console.log('originalEvent:', originalEvent)
      // 不允许拖拽
      if (e.draggedContext.element.num === 11) return false // false表示阻止,true表示不阻止
      return true;
    }
  }
}
</script>
<style lang="less" scoped>
.draggable {
  width: 1200px;
  height: 100vh;
  margin: 0 auto;
  .u-player {
    margin-top: 30px;
    display: inline-block;
    color: #0079DD;
    background: #F0F7FD;
    font-size: 16px;
    font-weight: 500;
    padding: 8px 18px;
    border-radius: 6px;
    text-align: center;
    cursor: move;
    &:not(:last-child) {
      margin-right: 16px;
    }
  }
}
.chosen { // 选择元素的样式
  background: #666 !important;
  color: #fff !important;
}
.ghost { // 目标位置占位符的样式及需要停靠位置的样式
  background: #1890FF !important;
}
</style>
相关文章
|
4月前
|
JavaScript
【vue】 vue2 自定义指令 实现全屏 、对话框拖拽
【vue】 vue2 自定义指令 实现全屏 、对话框拖拽
184 2
|
JavaScript 前端开发 容器
Vue antdv 下拉菜单不跟着滚动走(getPopupContainer 使用)
Vue antdv 下拉菜单不跟着滚动走(getPopupContainer 使用)
820 0
|
20天前
Vue3拖拽插件(vuedraggable@next)
vuedraggable 是一款 Vue2 拖拽插件,可轻松实现列表项的拖拽排序与交互。通过简单配置,即可在不同区域间拖动元素并实现数据同步。支持多种事件监听和自定义样式。
Vue3拖拽插件(vuedraggable@next)
|
1月前
在Vue3项目中使用 vue3-seamless-scroll 无缝滚动插件
本文介绍了如何在Vue3项目中使用`vue3-seamless-scroll`插件实现无缝滚动效果,并提供了详细的示例代码和运行效果。
720 0
|
4月前
|
存储 JavaScript 前端开发
用Vue写一个简单好看的菜单组件(Vue实战系列)
用Vue写一个简单好看的菜单组件(Vue实战系列)
194 2
|
4月前
|
JavaScript 前端开发 开发者
vue的组件和插件
Vue中的组件是用于构建用户界面的可复用单元,而插件是为Vue添加全局功能的扩展。两者在Vue框架中扮演着不同的角色,共同促进了Vue应用的开发和维护。
vue的组件和插件
|
4月前
|
JavaScript 前端开发
(详解)vue中实现主题切换的三种方式
(详解)vue中实现主题切换的三种方式
305 1
|
10月前
vue3里面vant组件的标签页使用?
vue3里面vant组件的标签页使用?
|
JavaScript 容器
开发一个简单的 Vue 弹窗组件
开发一个简单的 Vue 弹窗组件
82 1
|
前端开发