vue3使用vuedraggable和grid實(shí)現(xiàn)自定義拖拽布局方式
使用vuedraggable和grid實(shí)現(xiàn)自定義拖拽布局
實(shí)現(xiàn)思路
使用vuedraggable實(shí)現(xiàn)拖拽功能,拖拽改變的是模塊在list的順序,使用gird設(shè)置動(dòng)態(tài)類名,根據(jù)模塊在list的位置匹配對(duì)應(yīng)的gird樣式。
效果圖
每個(gè)模塊可以拖拽,不同數(shù)量和不同位置模塊寬度和高度不同(注意:模塊樣式width:100%,height:100%)

圖1-標(biāo)準(zhǔn)布局

圖2-三塊布局

圖3-自定義布局
<template>
<div class="wrap">
<div class="flex-row-start defineLayout">
<div class="flex-row" @click="changeLayout(LayoutType.FOUR)">
<div class="name">標(biāo)準(zhǔn)布局</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';
//資源對(duì)象
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]);
})
// 自定義當(dāng)前頁(yè)包含組件
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:
// 默認(rèn)四宮格
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);
}
}
// 設(shè)置拖拽組件的樣式
.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>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在vue-cli3.0 中使用預(yù)處理器 (Sass/Less/Stylus) 配置全局變量操作
這篇文章主要介紹了在vue-cli3.0 中使用預(yù)處理器 (Sass/Less/Stylus) 配置全局變量操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Vue中控制v-for循環(huán)次數(shù)的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Vue中控制v-for循環(huán)次數(shù)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue?select?change事件如何傳遞自定義參數(shù)
這篇文章主要介紹了vue?select?change事件如何傳遞自定義參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
詳解webpack編譯多頁(yè)面vue項(xiàng)目的配置問題
本篇文章主要介紹了詳解webpack編譯多頁(yè)面vue項(xiàng)目的配置問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
VueJs里利用CryptoJs實(shí)現(xiàn)加密及解密的方法示例
這篇文章主要介紹了VueJs里利用CryptoJs實(shí)現(xiàn)加密及解密的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
vue中this.$message的實(shí)現(xiàn)過(guò)程詳解
Message在開發(fā)中的使用頻率很高,也算是Element-UI組件庫(kù)中比較簡(jiǎn)單的,對(duì)于感興趣的朋友可以一起探討一下Message組件的實(shí)現(xiàn),本文詳細(xì)介紹了vue中this.$message的實(shí)現(xiàn)過(guò)程,感興趣的同學(xué)可以參考一下2023-04-04
Vue配合iView實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)的示例代碼
本篇文章主要介紹了Vue配合iView實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07

