vue3使用vuedraggable和grid实现自定义拖拽布局方式

 更新时间:2024年06月07日 09:22:24   作者:七月的冰红茶  
这篇文章主要介绍了vue3使用vuedraggable和grid实现自定义拖拽布局方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

使用vuedraggable和grid实现自定义拖拽布局

实现思路

使用vuedraggable实现拖拽功能,拖拽改变的是模块在list的顺序,使用gird设置动态类名,根据模块在list的位置匹配对应的gird样式。

效果图

每个模块可以拖拽,不同数量和不同位置模块宽度和高度不同(注意:模块样式width:100%,height:100%)

图1-标准布局

图2-三块布局

图3-自定义布局

<template>
  <div class="wrap">
    <div class="flex-row-start defineLayout">
      <div class="flex-row" @click="changeLayout(LayoutType.FOUR)">
        <div class="name">标准布局</div>
      </div>
      <div class="flex-row" @click="changeLayout(LayoutType.THREE)">
        <div class="name">三块布局</div>
      </div>
      <el-dropdown ref="dropdown1" trigger="contextmenu" style="margin-right: 30px">
        <div class="flex-row el-dropdown-link" @click="() => {
          if (dropdown1) dropdown1.handleOpen();
        }">
          <div class="name">自定义布局</div>
        </div>
        <template #dropdown>
          <el-checkbox-group class="flex-col-start" v-model="checkedIdList" :max="4" style="padding: 10px 0 10px 30px;">
            <el-checkbox @change="changeLayout(LayoutType.DEFINE)" v-for="(item, index) of cList" :key="index"
              :label='item.id'>{{
                item.name }}</el-checkbox>
          </el-checkbox-group>
        </template>
      </el-dropdown>
    </div>
    <div class="draggable-border">
      <draggable :list="moduleList" item-key="id" :options="{ animation: 200, ghostClass: 'ghost' }" :class="{
        gird1col: moduleList.length == 1,
        gird2col: moduleList.length == 2,
        gird3col: moduleList.length == 3,
        gird4col: moduleList.length == 4
      }">
        <template #item="{ element, index }">
          <component :ref="element.ref" :is="element.component" :name="element.name" :class="{
            [`dragItem${index}`]: true,
          }">
          </component>
        </template>
      </draggable>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { useState, useMutations } from "@/utils/useStore";
import Block1 from '@/components/block1/block1';
import Block2 from '@/components/block2/block2';
import Block3 from '@/components/block3/block3';
import Block4 from '@/components/block4/block4';
import Block5 from '@/components/block5/block5.vue';

//@ts-ignore
import draggable from "vuedraggable";
import { LayoutType } from '@/utils/enum';

//资源对象
let resource = ref<any>();
//@ts-ignore
const { moduleList } = useState(["moduleList"], "drag");
//@ts-ignore
const { setModuleList } = useMutations(["setModuleList"], "drag");

let dropdown1 = ref();
let checkedIdList = ref<number[]>([]);//自定义选择的模块

let cList: any[] = [
  {
    type: '1',
    id: 1,
    name: '块1',
    component: Block1
  }, {
    type: '1',
    id: 2,
    name: '块2',
    component: Block2
  }, {
    type: '2',
    id: 3,
    name: '块3',
    component: Block3
  }, {
    type: '2',
    id: 4,
    name: '块4',
    component: Block4
  }, {
    type: '2',
    id: 5,
    name: '块5',
    component: Block5
  },
];

onMounted(() => {
  setCompontent([1, 2, 3, 4]);
})

// 自定义当前页包含组件
const setCompontent = (idList: number[]) => {
  checkedIdList.value = idList;
  let list = cList.filter((f: any) => {
    return idList.indexOf(f.id) != -1;
  });
  setModuleList(list);
  console.log("list", list);
};

// 切换布局
const changeLayout = (type) => {
  switch (type) {
    case LayoutType.THREE:
      setCompontent([1, 2, 5]);
      break;
    case LayoutType.DEFINE:
      if (checkedIdList.value) setCompontent(checkedIdList.value);
      break;
    default:
      // 默认四宫格
      setCompontent([1, 2, 3, 4]);
      break;
  }
}
</script>
<style scoped lang="scss">
.wrap {
  height: 100vh;
  width: 100vw;
  position: relative;
  display: block;
  overflow: hidden;

  .defineLayout {
    color: #fff;
    height: 41px;
    width: 100%;
    background-color: #000;
    align-items: center;
    padding: 0 20px;

    .name {
      font-size: 12px;
      font-weight: 500;
      color: #FFFFFF;
      line-height: 17px;
      margin-left: 5px;
      margin-right: 20px;
      cursor: pointer;
    }
  }

  .draggable-border {
    background-color: #fff;
    width: 100%;
    height: calc(100vh - 41px);
  }
}

// 设置拖拽组件的样式
.draggable-border>div {
  width: 100%;
  height: 100%;
  display: grid;
  grid:
    'one two'
    'three four';
  grid-template-columns: 50% 50%;
  grid-template-rows: 50% 50%;
}

.gird4col {
  grid:
    'one two'
    'three four' !important;
  grid-template-columns: 50% 50% !important;
  grid-template-rows: 50% 50% !important;
}

.gird3col {
  grid:
    'one three'
    'two three' !important;
  grid-template-columns: 50% 50% !important;
  grid-template-rows: 50% 50% !important;
}

.gird2col {
  grid:
    'one two'
    'one two' !important;
  grid-template-columns: 50% 50% !important;
  grid-template-rows: 50% 50% !important;
}

.gird1col {
  grid:
    'one' !important;
  grid-template-columns: 100% !important;
  grid-template-rows: 100% !important;
}

.fullscreen {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}

.dragItem0 {
  grid-area: one;
}

.dragItem1 {
  grid-area: two;
}

.dragItem2 {
  grid-area: three;
}

.dragItem3 {
  grid-area: four;
}
</style>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Element-UI 10个技巧小结

    Element-UI 10个技巧小结

    本文主要介绍了Element-UI 10个技巧小结,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • 记录vue项目中遇到的一点小问题

    记录vue项目中遇到的一点小问题

    本文是脚本之家小编给大家收藏整理的关于vue项目中遇到的一点小问题,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • 在vue-cli3.0 中使用预处理器 (Sass/Less/Stylus) 配置全局变量操作

    在vue-cli3.0 中使用预处理器 (Sass/Less/Stylus) 配置全局变量操作

    这篇文章主要介绍了在vue-cli3.0 中使用预处理器 (Sass/Less/Stylus) 配置全局变量操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • Vue中控制v-for循环次数的实现方法

    Vue中控制v-for循环次数的实现方法

    今天小编就为大家分享一篇Vue中控制v-for循环次数的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • vue select change事件如何传递自定义参数

    vue select change事件如何传递自定义参数

    这篇文章主要介绍了vue select change事件如何传递自定义参数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • Vue中如何避免props验证失败问题

    Vue中如何避免props验证失败问题

    本文将探讨 props 验证失败的常见原因,并提供解决方法,帮助开发者在 Vue 组件开发中避免这些问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • 详解webpack编译多页面vue项目的配置问题

    详解webpack编译多页面vue项目的配置问题

    本篇文章主要介绍了详解webpack编译多页面vue项目的配置问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • VueJs里利用CryptoJs实现加密及解密的方法示例

    VueJs里利用CryptoJs实现加密及解密的方法示例

    这篇文章主要介绍了VueJs里利用CryptoJs实现加密及解密的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-04-04
  • vue中this.$message的实现过程详解

    vue中this.$message的实现过程详解

    Message在开发中的使用频率很高,也算是Element-UI组件库中比较简单的,对于感兴趣的朋友可以一起探讨一下Message组件的实现,本文详细介绍了vue中this.$message的实现过程,感兴趣的同学可以参考一下
    2023-04-04
  • Vue配合iView实现省市二级联动的示例代码

    Vue配合iView实现省市二级联动的示例代码

    本篇文章主要介绍了Vue配合iView实现省市二级联动的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07

最新评论