Vue3可拖拽调整大小弹框

<template>
  <div>
    <div v-if="visible" class="modal-overlay">
      <div 
        class="modal-container"
        ref="modalEl"
        :style="{
          width: size.width + 'px',
          height: size.height + 'px',
          left: position.x + 'px',
          top: position.y + 'px'
        }"
      >
        <div class="modal-header" @mousedown="startDrag">
          <div class="modal-title">{{ title }}</div>
          <button class="modal-close" @click="closeModal">×</button>
        </div>
        
        <div class="modal-content">
          <slot></slot>
        </div>
        
        <slot name="footer">
          <div class="footer">
            <el-button @click="closeModal">取消</el-button>
            <el-button type="primary" @click="confirmClick">确定</el-button>
          </div>
        </slot>
        
        <!-- 调整大小的手柄 -->
        <div class="resize-handle resize-handle-n" @mousedown="(e) => startResize(e, 'n')"></div>
        <div class="resize-handle resize-handle-s" @mousedown="(e) => startResize(e, 's')"></div>
        <div class="resize-handle resize-handle-e" @mousedown="(e) => startResize(e, 'e')"></div>
        <div class="resize-handle resize-handle-w" @mousedown="(e) => startResize(e, 'w')"></div>
        <div class="resize-handle resize-handle-ne" @mousedown="(e) => startResize(e, 'ne')"></div>
        <div class="resize-handle resize-handle-nw" @mousedown="(e) => startResize(e, 'nw')"></div>
        <div class="resize-handle resize-handle-se" @mousedown="(e) => startResize(e, 'se')"></div>
        <div class="resize-handle resize-handle-sw" @mousedown="(e) => startResize(e, 'sw')"></div>
      </div>
    </div>
  </div>
    
      
</template>

<script setup>
import { ref, watch } from 'vue';


const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '弹窗'
  },
  width: {
    type: Number,
    default: 400
  },
  height: {
    type: Number,
    default: 300
  }
})

const emit = defineEmits(['update:modelValue', 'close', 'confirm'])

const visible = ref(props.modelValue);
const modalEl = ref(null);

// 位置和尺寸状态
const position = ref({ x: 0, y: 0 });
const size = ref({ width: props.width, height: props.height });

// 拖拽状态
const isDragging = ref(false);
const dragStart = ref({ x: 0, y: 0 });

// 调整大小状态
const isResizing = ref(false);
const resizeDir = ref('');
const resizeStart = ref({ x: 0, y: 0, width: 0, height: 0, left: 0, top: 0 });

// 监听modelValue变化
watch(() => props.modelValue, (val) => {
  visible.value = val;
  if (val) {
    // 居中显示
    position.value = {
      x: (window.innerWidth - size.value.width) / 2,
      y: (window.innerHeight - size.value.height) / 2
    };
  }
});

// 开始拖拽
const startDrag = (e) => {
  isDragging.value = true;
  dragStart.value = {
    x: e.clientX - position.value.x,
    y: e.clientY - position.value.y
  };
  
  document.addEventListener('mousemove', onDrag);
  document.addEventListener('mouseup', stopDrag);
};

// 拖拽中
const onDrag = (e) => {
  if (!isDragging.value) return;
  
  position.value = {
    x: e.clientX - dragStart.value.x,
    y: e.clientY - dragStart.value.y
  };
};

// 停止拖拽
const stopDrag = () => {
  isDragging.value = false;
  document.removeEventListener('mousemove', onDrag);
  document.removeEventListener('mouseup', stopDrag);
};

// 开始调整大小
const startResize = (e, dir) => {
  isResizing.value = true;
  resizeDir.value = dir;
  resizeStart.value = {
    x: e.clientX,
    y: e.clientY,
    width: size.value.width,
    height: size.value.height,
    left: position.value.x,
    top: position.value.y
  };
  
  document.addEventListener('mousemove', onResize);
  document.addEventListener('mouseup', stopResize);
  e.preventDefault();
};

// 调整大小中
const onResize = (e) => {
  if (!isResizing.value) return;
  
  const deltaX = e.clientX - resizeStart.value.x;
  const deltaY = e.clientY - resizeStart.value.y;
  
  let newWidth = resizeStart.value.width;
  let newHeight = resizeStart.value.height;
  let newX = position.value.x;
  let newY = position.value.y;
  
  switch (resizeDir.value) {
    case 'e':
      newWidth = Math.max(200, resizeStart.value.width + deltaX);
      break;
    case 'w':
      newWidth = Math.max(200, resizeStart.value.width - deltaX);
      newX = resizeStart.value.left + deltaX;
      break;
    case 's':
      newHeight = Math.max(150, resizeStart.value.height + deltaY);
      break;
    case 'n':
      newHeight = Math.max(150, resizeStart.value.height - deltaY);
      newY = resizeStart.value.top + deltaY;
      break;
    case 'se':
      newWidth = Math.max(200, resizeStart.value.width + deltaX);
      newHeight = Math.max(150, resizeStart.value.height + deltaY);
      break;
    case 'sw':
      newWidth = Math.max(200, resizeStart.value.width - deltaX);
      newHeight = Math.max(150, resizeStart.value.height + deltaY);
      newX = resizeStart.value.left + deltaX;
      break;
    case 'ne':
      newWidth = Math.max(200, resizeStart.value.width + deltaX);
      newHeight = Math.max(150, resizeStart.value.height - deltaY);
      newY = resizeStart.value.top + deltaY;
      break;
    case 'nw':
      newWidth = Math.max(200, resizeStart.value.width - deltaX);
      newHeight = Math.max(150, resizeStart.value.height - deltaY);
      newX = resizeStart.value.left + deltaX;
      newY = resizeStart.value.top + deltaY;
      break;
  }
  
  size.value = { width: newWidth, height: newHeight };
  position.value = { x: newX, y: newY };
};

// 停止调整大小
const stopResize = () => {
  isResizing.value = false;
  document.removeEventListener('mousemove', onResize);
  document.removeEventListener('mouseup', stopResize);
};

// 关闭弹窗
const closeModal = () => {
  visible.value = false;
  emit('update:modelValue', false);
  emit('close');
};

// 点确定
const confirmClick = () => {
  emit('confirm');
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

.modal-container {
  background: white;
  border-radius: 6px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: absolute;
  min-width: 300px;
  min-height: 200px;
  animation: scaleIn 0.3s ease;
}

.modal-header {
  padding: 16px 20px;
  /* background: #3498db; */
  /* color: white; */
  cursor: move;
  display: flex;
  justify-content: space-between;
  align-items: center;
  user-select: none;
}

.modal-title {
  font-weight: 500;
  font-size: 18px;
}

.modal-close {
  background: none;
  border: none;
  /* color: white; */
  font-size: 24px;
  cursor: pointer;
  line-height: 1;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.2s;
}

.modal-close:hover {
  background: rgba(255, 255, 255, 0.2);
}

.modal-content {
  padding: 0px 20px 20px 20px ;
  flex: 1;
  overflow: auto;
}

.resize-handle {
  position: absolute;
  background: transparent;
  z-index: 10;
}

.resize-handle-n {
  top: -4px;
  left: 10px;
  right: 10px;
  height: 8px;
  cursor: n-resize;
}

.resize-handle-s {
  bottom: -4px;
  left: 10px;
  right: 10px;
  height: 8px;
  cursor: s-resize;
}

.resize-handle-e {
  top: 10px;
  right: -4px;
  width: 8px;
  bottom: 10px;
  cursor: e-resize;
}

.resize-handle-w {
  top: 10px;
  left: -4px;
  width: 8px;
  bottom: 10px;
  cursor: w-resize;
}

.resize-handle-ne {
  top: -4px;
  right: -4px;
  width: 12px;
  height: 12px;
  cursor: ne-resize;
}

.resize-handle-nw {
  top: -4px;
  left: -4px;
  width: 12px;
  height: 12px;
  cursor: nw-resize;
}

.resize-handle-se {
  bottom: -4px;
  right: -4px;
  width: 12px;
  height: 12px;
  cursor: se-resize;
}

.resize-handle-sw {
  bottom: -4px;
  left: -4px;
  width: 12px;
  height: 12px;
  cursor: sw-resize;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes scaleIn {
  from { 
    opacity: 0;
    transform: scale(0.9); 
  }
  to { 
    opacity: 1;
    transform: scale(1); 
  }
}

.features {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  justify-content: center;
  margin-top: 40px;
}

.feature-card {
  background: white;
  border-radius: 10px;
  padding: 20px;
  width: 250px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.feature-icon {
  font-size: 40px;
  margin-bottom: 15px;
  color: #3498db;
}

.feature-title {
  font-weight: 600;
  margin-bottom: 10px;
  color: #2c3e50;
}

.feature-desc {
  color: #7f8c8d;
  font-size: 14px;
}

.debug-info {
  position: fixed;
  bottom: 20px;
  left: 20px;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 10px;
  border-radius: 5px;
  font-family: monospace;
  font-size: 14px;
}

.footer {
  display: flex;
  justify-content: end;
  align-items: center;
  margin: 10px;
}
</style>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
最近更新时间: 2025/08/20 08:21:41
最近更新
01
2025/07/18 00:00:00
02
2025/05/05 00:00:00
03
2025/05/01 00:00:00