写一个在vue中拖拽div盒子的边框可以改变它的大小的代码
时间: 2023-06-04 20:05:29 浏览: 773
好的,这是一个简单的可以在Vue中拖拽div盒子边框改变大小的代码:
```
<template>
<div class="dragbox" :style="{ width: width + 'px', height: height + 'px', left: left + 'px', top: top + 'px' }" ref="box" @mousedown="startDrag">
<div class="dragbox-border top" @mousedown="startResize('top')"></div>
<div class="dragbox-border right" @mousedown="startResize('right')"></div>
<div class="dragbox-border bottom" @mousedown="startResize('bottom')"></div>
<div class="dragbox-border left" @mousedown="startResize('left')"></div>
<div class="dragbox-content">
<!-- 你的div盒子内容 -->
</div>
</div>
</template>
<script>
export default {
data() {
return {
left: 0,
top: 0,
width: 200,
height: 200,
startX: 0,
startY: 0,
startLeft: 0,
startTop: 0,
resizing: false,
resizeDirection: ''
};
},
methods: {
startDrag(event) {
event.preventDefault();
this.startX = event.clientX;
this.startY = event.clientY;
this.startLeft = this.left;
this.startTop = this.top;
this.resizing = false;
document.addEventListener('mousemove', this.drag);
document.addEventListener('mouseup', this.stopDrag);
},
drag(event) {
event.preventDefault();
const deltaX = event.clientX - this.startX;
const deltaY = event.clientY - this.startY;
this.left = this.startLeft + deltaX;
this.top = this.startTop + deltaY;
},
stopDrag(event) {
document.removeEventListener('mousemove', this.drag);
document.removeEventListener('mouseup', this.stopDrag);
},
startResize(direction) {
event.preventDefault();
this.startX = event.clientX;
this.startY = event.clientY;
this.startWidth = this.width;
this.startHeight = this.height;
this.resizing = true;
this.resizeDirection = direction;
document.addEventListener('mousemove', this.resize);
document.addEventListener('mouseup', this.stopResize);
},
resize(event) {
event.preventDefault();
const deltaX = event.clientX - this.startX;
const deltaY = event.clientY - this.startY;
if (this.resizeDirection === 'left') {
this.width = this.startWidth - deltaX;
this.left = this.startLeft + deltaX;
}
if (this.resizeDirection === 'top') {
this.height = this.startHeight - deltaY;
this.top = this.startTop + deltaY;
}
if (this.resizeDirection === 'right') {
this.width = this.startWidth + deltaX;
}
if (this.resizeDirection === 'bottom') {
this.height = this.startHeight + deltaY;
}
},
stopResize(event) {
document.removeEventListener('mousemove', this.resize);
document.removeEventListener('mouseup', this.stopResize);
}
}
};
</script>
<style scoped>
.dragbox {
position: absolute;
border: 1px solid black;
cursor: move;
}
.dragbox-border {
position: absolute;
width: 10px;
height: 10px;
background-color: black;
opacity: 0.5;
cursor: pointer;
}
.dragbox-border.top {
top: -5px;
left: 50%;
transform: translateX(-50%);
}
.dragbox-border.right {
top: 50%;
right: -5px;
transform: translateY(-50%);
}
.dragbox-border.bottom {
bottom: -5px;
left: 50%;
transform: translateX(-50%);
}
.dragbox-border.left {
top: 50%;
left: -5px;
transform: translateY(-50%);
}
.dragbox-content {
padding: 10px;
}
</style>
```
阅读全文