npm install --save vue-draggable-resizable
import VueDraggableResizable from "vue-draggable-resizable";
export default {
components: {
VueDraggableResizable,
}
}
<VueDraggableResizable></VueDraggableResizable>
< VueDraggableResizable class="我的类" >
< VueDraggableResizable :disable-user-select ="false">
< VueDraggableResizable :draggable ="false" >
< VueDraggableResizable :resizable =" false ">
< VueDraggableResizable :w =" 200 " :h =" 200 ">
< VueDraggableResizable :x =" 0 " :y =" 0 " >
< VueDraggableResizable @dragging =" onDragging ">
< VueDraggableResizable @dragstop =" onDragstop ">
//vue页面
<template>
<div class="box" ref="box">
<VueDraggableResizable
:w="w"
:h="h"
:x="item.x"
:y="item.y"
:parent="true"
class="drag"
:resizable="true"
:onDragStart="() => dragStart(item.id)"
@dragstop="onDragStop"
@dragging="onDragging"
@resizing="onResizing"
v-for="item in showComponent"
:key="item.id"
:class="{ color: item.color }"
>
<div class="dragbox">
{{ item.name }}<br />
X:{{ item.x }} / Y:{{ item.y }} - 宽度:{{ w }} / 高度:{{ h }}
</div>
</VueDraggableResizable>
</div>
</template>
<script>
import VueDraggableResizable from "vue-draggable-resizable";
export default {
data() {
return {
w: 400,
h: 100,
draggingId: 0,
showComponent: [
{ id: 1, show: false, name: "aa", y: 200, x: 100 },
{ id: 2, show: false, name: "nn", y: 400, x: 100 },
],
};
},
components: {
VueDraggableResizable,
},
mounted() {},
methods: {
dragStart(id) {
this.draggingId = id;
},
onDragStop(x, y) {
const item = this.showComponent.find((i) => i.id === this.draggingId);
item.x = x;
item.y = y;
},
onDragging(x, y) {
const item = this.showComponent.find((i) => i.id === this.draggingId);
item.x = x;
item.y = y;
},
onResizing(ax, ay, aw, ah) {
console.log(ax, ay, aw, ah);
},
drag(params) {
console.log(params);
},
},
};
</script>
<style scoped>
.box {
width: 800px;
position: relative;
height: 100vh;
border: 1px solid #000;
}
.drag {
position: absolute;
top: 0;
left: 0;
color: #fff;
background-color: blue;
display: flex;
}
</style>