vue项目中 五小一大六宫格拖拽排序,6宫格拖拽换位,矩阵拖拽排序

背景:项目中用到1、4、6、9、12、16宫格排序,拖拽换位等场景,项目是监控的视频矩阵,用户矩阵拖拽排序换位,之前已经实现1、4、9、12、16等矩阵式排列的拖拽排序,但是六宫格要求五小一大,之前的基于vue-grid-layout开发的拖拽对换位置已经实现除了五小一大的

需求图:五小一大互相可以拖拽换位排序

实现方案:

基于vue-dragging https://github.com/hilongjw/vue-dragging

实现六宫格拖拽,该组件也可实现1,4、6、9等需要简单修改即可,下面分享一下构建六宫格的思路

第一步:在vue项目中安装 vue-dragging

npm install awe-dnd --save

第二步:在main.js中引用

import VueDND from 'awe-dnd'
Vue.use(VueDND)

第三步:在vue组件中使用

<template>
  <div class="other-group">
    <h2>五小一大拖拽</h2>
    <div class="des">模块拖拽,不能重新渲染dom,不能改变数据结构</div>
    <br>
    <div class="color-show">
      <div v-for="color in colors2" v-dragging="{ list: colors2, item: color, group: 'color2' }" class="color-box" :style="{'background-color': color.text}"
        :key="color.text">
        {{color.text}}
      </div>
    </div>
  </div>
</template>
<!--your.vue-->
<script>
export default {
  data() {
    return {
      colors2: [
        { text: "Aquamarine" },
        { text: "Hotpink" },
        { text: "Gold" },
        { text: "Crimson" },
        { text: "Blueviolet" },
        { text: "rgb(0,100,"+Math.random()*100+")" }
      ],
    }
  },
  //   if your need multi drag
  mounted: function () {

    this.$dragging.$on('dragged', ({ value }) => {
      console.log(value.item)
      console.log(value.list)
      console.log(value.otherData)
    })


    this.$dragging.$on('dragend', function (data) {
      console.log('dragend', data)
    })
  }
}
</script>

<style scoped>
.other-group,
.multi-group {
  margin-left: 20px;
}

.color-show {
  /* display: flex;
  flex-wrap: wrap; */
  width: 30rem;
  margin: 0 auto;
}
.color-box {
  float: left;
  width: 33%;
  height: 6rem;
  background: #efefef;
  line-height: 6rem;
  text-align: center;
  color: #fff;
  transition: transform 0.3s;
}
.color-box:first-child {
  width: 66%;
  height: 12rem;
}
.color-box.dragging {
  transform: scale(1.1);
}
</style>

其他宫格例如:九宫格 只需要修改 color2数组长度和修改对应的样式即可

九宫格code:

<template>
  <div class="other-group">
    <h2>九宫格拖拽</h2>
    <div class="des">模块拖拽,不能重新渲染dom,不能改变数据结构</div>
    <br>
    <div class="color-show">
      <div v-for="color in colors2" v-dragging="{ list: colors2, item: color, group: 'color2' }" class="color-box" :style="{'background-color': color.text}"
        :key="color.text">
        {{color.text}}
      </div>
    </div>
  </div>
</template>
<!--your.vue-->
<script>
export default {
  data() {
    return {
      colors2: [
        { text: "Aquamarine" },
        { text: "Hotpink" },
        { text: "Gold" },
        { text: "Crimson" },
        { text: "Blueviolet" },
        { text: "Lightblue" },
        { text: "green" },
        { text: "red" },
        { text: "rgb(0,100," + Math.random() * 100 + ")" }
      ],
    }
  },
  //   if your need multi drag
  mounted: function () {

    this.$dragging.$on('dragged', ({ value }) => {
      console.log(value.item)
      console.log(value.list)
      console.log(value.otherData)
    })


    this.$dragging.$on('dragend', function (data) {
      console.log('dragend', data)
    })
  }
}
</script>

<style scoped>
.other-group,
.multi-group {
  margin-left: 20px;
}

.color-show {
  display: flex;
  flex-wrap: wrap;
  width: 30rem;
  margin: 0 auto;
}
.color-box {
  float: left;
  width: 33%;
  height: 6rem;
  background: #efefef;
  line-height: 6rem;
  text-align: center;
  color: #fff;
  transition: transform 0.3s;
}

.color-box.dragging {
  transform: scale(1.1);
}
</style>

在vue3.x中使用请看这一篇文章

vue项目中,兼容vue3.0 & 1.0 & 2.0 宫格拖拽排序,特殊五小一大六宫格拖拽icon-default.png?t=LBL2https://mp.csdn.net/mp_blog/creation/editor/122545182

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随便起的名字也被占用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值