请使用 vue和element-ui js mouseevent事件 实现弹窗 的四个角和四条边进行放大缩小,其他属性有 v-dialogDrag, :modal="false", :close-on-click-modal="false",弹窗默认显示,请用具体代码实现
时间: 2024-02-18 11:02:23 浏览: 138
好的,我可以为您提供相关的代码示例:
```html
<template>
<div>
<el-button type="primary" @click="dialogVisible = true">点击打开弹窗</el-button>
<el-dialog
:visible.sync="dialogVisible"
:modal="false"
:close-on-click-modal="false"
v-dialogDrag
class="resizable-dialog"
>
<div class="resize-handle top-left" @mousedown="startResize($event, 'topLeft')"></div>
<div class="resize-handle top-right" @mousedown="startResize($event, 'topRight')"></div>
<div class="resize-handle bottom-left" @mousedown="startResize($event, 'bottomLeft')"></div>
<div class="resize-handle bottom-right" @mousedown="startResize($event, 'bottomRight')"></div>
<div class="resize-handle top" @mousedown="startResize($event, 'top')"></div>
<div class="resize-handle right" @mousedown="startResize($event, 'right')"></div>
<div class="resize-handle bottom" @mousedown="startResize($event, 'bottom')"></div>
<div class="resize-handle left" @mousedown="startResize($event, 'left')"></div>
<span slot="title">可调整大小的弹窗</span>
<p>在这里可以添加弹窗的内容</p>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
dialogVisible: false,
resizing: false,
resizeType: null,
startX: null,
startY: null,
startWidth: null,
startHeight: null
}
},
methods: {
startResize(event, type) {
this.resizing = true
this.resizeType = type
this.startX = event.clientX
this.startY = event.clientY
this.startWidth = this.$refs.dialog.$el.offsetWidth
this.startHeight = this.$refs.dialog.$el.offsetHeight
},
endResize() {
this.resizing = false
this.resizeType = null
this.startX = null
this.startY = null
this.startWidth = null
this.startHeight = null
},
handleMouseMove(event) {
if (this.resizing) {
const deltaX = event.clientX - this.startX
const deltaY = event.clientY - this.startY
let newWidth = this.startWidth
let newHeight = this.startHeight
let newLeft = this.$refs.dialog.$el.offsetLeft
let newTop = this.$refs.dialog.$el.offsetTop
switch (this.resizeType) {
case 'topLeft':
newWidth -= deltaX
newHeight -= deltaY
newLeft += deltaX
newTop += deltaY
break
case 'topRight':
newWidth += deltaX
newHeight -= deltaY
newTop += deltaY
break
case 'bottomLeft':
newWidth -= deltaX
newHeight += deltaY
newLeft += deltaX
break
case 'bottomRight':
newWidth += deltaX
newHeight += deltaY
break
case 'top':
newHeight -= deltaY
newTop += deltaY
break
case 'right':
newWidth += deltaX
break
case 'bottom':
newHeight += deltaY
break
case 'left':
newWidth -= deltaX
newLeft += deltaX
break
}
this.$refs.dialog.$el.style.width = newWidth + 'px'
this.$refs.dialog.$el.style.height = newHeight + 'px'
this.$refs.dialog.$el.style.left = newLeft + 'px'
this.$refs.dialog.$el.style.top = newTop + 'px'
}
}
},
mounted() {
document.addEventListener('mouseup', this.endResize)
document.addEventListener('mousemove', this.handleMouseMove)
},
beforeDestroy() {
document.removeEventListener('mouseup', this.endResize)
document.removeEventListener('mousemove', this.handleMouseMove)
}
}
</script>
<style>
.resizable-dialog {
position: absolute !important;
top: 50% !important;
left: 50% !important;
transform: translate(-50%, -50%) !important;
}
.resize-handle {
position: absolute;
width: 10px;
height: 10px;
background-color: #999;
}
.resize-handle.top-left {
top: -5px;
left: -5px;
cursor: nwse-resize;
}
.resize-handle.top-right {
top: -5px;
right: -5px;
cursor: nesw-resize;
}
.resize-handle.bottom-left {
bottom: -5px;
left: -5px;
cursor: nesw-resize;
}
.resize-handle.bottom-right {
bottom: -5px;
right: -5px;
cursor: nwse-resize;
}
.resize-handle.top {
top: -5px;
left: 50%;
transform: translateX(-50%);
cursor: ns-resize;
}
.resize-handle.right {
top: 50%;
right: -5px;
transform: translateY(-50%);
cursor: ew-resize;
}
.resize-handle.bottom {
bottom: -5px;
left: 50%;
transform: translateX(-50%);
cursor: ns-resize;
}
.resize-handle.left {
top: 50%;
left: -5px;
transform: translateY(-50%);
cursor: ew-resize;
}
</style>
```
在这个示例中,我们使用了 `v-dialogDrag` 指令使弹窗可以被拖拽,同时添加了可以调整大小的功能。具体地,我们在弹窗的四个角和四条边添加了可拖拽的缩放手柄,并在鼠标按下时监听 `mousedown` 事件,记录下当前的位置信息和弹窗的尺寸,然后在鼠标移动时根据手柄的位置和拖拽的距离计算出新的尺寸和位置,并更新弹窗的样式。在松开鼠标时,我们要将缩放状态重置回原始状态。同时,我们使用了 `@mousemove` 和 `@mouseup` 事件在全局范围内监听鼠标移动和松开的事件,以避免在拖拽时鼠标移动出了弹窗的范围。
阅读全文