vue3-drr-grid-layou 设置 prevent-collision 属性可以防止单元格碰撞,就是拖动A1单元格移动到A2单元格时,不会改变A2单元格的位置,拖动时无法改变其他元素的位置。
<!-- 设置 prevent-collision true防止碰撞 -->
<grid-layout :prevent-collision="true" > </grid-layout>
<template>
<div >拖动时无法改变其他元素的位置</div>
<div id="content">
<grid-layout
v-model:layout="layout"
:col-num="12"
:row-height="30"
style="width: 100vw"
:prevent-collision="true"
>
<template #default="{ gridItemProps }">
<grid-item
v-for="item in layout"
:key="item.i"
v-bind="gridItemProps"
:x="item.x"
:y="item.y"
:w="item.w"
:h="item.h"
:i="item.i"
>
<div class="item">
<label>{{ item.i }}</label>
</div>
</grid-item>
</template>
</grid-layout>
</div>
</template>
<script setup lang="ts">
/*
vue3-drr-grid-layout 中文文档
https://www.itxst.com/vue3-drr-grid-layout/tutorial.html
*/
import { getCurrentInstance, onMounted, ref } from "vue";
import { GridLayout, GridItem } from "vue3-drr-grid-layout";
import "vue3-drr-grid-layout/dist/style.css";
const _gridlayout = ref(null);
const layout = ref([
{ x: 0, y: 0, w: 2, h: 2, i: 0 },
{ x: 2, y: 0, w: 2, h: 2, i: 1 },
{ x: 4, y: 0, w: 2, h: 2, i: 2 },
{ x: 0, y: 1, w: 6, h: 2, i: 3 },
]);
</script>
<style scoped>
.droppable-element {
width: 120px;
text-align: center;
background: #fdd;
border: 1px solid black;
margin: 10px 0;
padding: 10px;
}
.item {
display: flex;
justify-content: space-between;
}
.item > span {
margin-left: 10px;
padding: 1px;
border: solid 1px #ddd;
cursor: pointer;
background-color: rgb(171, 171, 171);
color: #000;
font-size: 11px;
}
</style>