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
| <template> <div v-if="visible" ref="floatWindow" class="float-window" :style="windowStyle" @mousedown="handleMouseDown" > <!-- 窗口头部 --> <div class="window-header" @mousedown="startDrag"> <div class="window-title">{{ title }}</div> <div class="window-controls"> <button class="control-btn" @click="minimize">-</button> <button class="control-btn" @click="close">×</button> </div> </div> <!-- 窗口内容 --> <div class="window-content"> <slot></slot> </div> <!-- 调节大小的控制点 --> <div class="resize-handles"> <div class="resize-handle resize-n" @mousedown="startResize('n')"></div> <div class="resize-handle resize-s" @mousedown="startResize('s')"></div> <div class="resize-handle resize-w" @mousedown="startResize('w')"></div> <div class="resize-handle resize-e" @mousedown="startResize('e')"></div> <div class="resize-handle resize-nw" @mousedown="startResize('nw')"></div> <div class="resize-handle resize-ne" @mousedown="startResize('ne')"></div> <div class="resize-handle resize-sw" @mousedown="startResize('sw')"></div> <div class="resize-handle resize-se" @mousedown="startResize('se')"></div> </div> </div> </template>
<script> export default { name: 'FloatWindow', props: { title: { type: String, default: '悬浮窗口' }, initialWidth: { type: Number, default: 400 }, initialHeight: { type: Number, default: 300 }, initialX: { type: Number, default: 100 }, initialY: { type: Number, default: 100 }, minWidth: { type: Number, default: 200 }, minHeight: { type: Number, default: 150 }, visible: { type: Boolean, default: true } }, data() { return { // 窗口位置和大小 x: this.initialX, y: this.initialY, width: this.initialWidth, height: this.initialHeight, // 拖拽状态 isDragging: false, dragStartX: 0, dragStartY: 0, windowStartX: 0, windowStartY: 0, // 调节大小状态 isResizing: false, resizeDirection: '', resizeStartX: 0, resizeStartY: 0, resizeStartWidth: 0, resizeStartHeight: 0, resizeStartLeft: 0, resizeStartTop: 0 } }, computed: { windowStyle() { return { left: this.x + 'px', top: this.y + 'px', width: this.width + 'px', height: this.height + 'px' } } }, mounted() { // 添加全局事件监听 document.addEventListener('mousemove', this.handleMouseMove) document.addEventListener('mouseup', this.handleMouseUp) }, beforeDestroy() { // 移除全局事件监听 document.removeEventListener('mousemove', this.handleMouseMove) document.removeEventListener('mouseup', this.handleMouseUp) }, methods: { // 开始拖拽 startDrag(e) { e.preventDefault() this.isDragging = true this.dragStartX = e.clientX this.dragStartY = e.clientY this.windowStartX = this.x this.windowStartY = this.y }, // 开始调节大小 startResize(direction) { this.isResizing = true this.resizeDirection = direction this.resizeStartX = event.clientX this.resizeStartY = event.clientY this.resizeStartWidth = this.width this.resizeStartHeight = this.height this.resizeStartLeft = this.x this.resizeStartTop = this.y }, // 鼠标移动处理 handleMouseMove(e) { if (this.isDragging) { this.handleDrag(e) } else if (this.isResizing) { this.handleResize(e) } }, // 处理拖拽 handleDrag(e) { const deltaX = e.clientX - this.dragStartX const deltaY = e.clientY - this.dragStartY let newX = this.windowStartX + deltaX let newY = this.windowStartY + deltaY // 边界检测 const maxX = window.innerWidth - this.width const maxY = window.innerHeight - this.height newX = Math.max(0, Math.min(newX, maxX)) newY = Math.max(0, Math.min(newY, maxY)) this.x = newX this.y = newY }, // 处理调节大小 handleResize(e) { const deltaX = e.clientX - this.resizeStartX const deltaY = e.clientY - this.resizeStartY let newWidth = this.resizeStartWidth let newHeight = this.resizeStartHeight let newX = this.resizeStartLeft let newY = this.resizeStartTop // 根据调节方向计算新的尺寸和位置 switch (this.resizeDirection) { case 'n': newHeight = this.resizeStartHeight - deltaY newY = this.resizeStartTop + deltaY break case 's': newHeight = this.resizeStartHeight + deltaY break case 'w': newWidth = this.resizeStartWidth - deltaX newX = this.resizeStartLeft + deltaX break case 'e': newWidth = this.resizeStartWidth + deltaX break case 'nw': newWidth = this.resizeStartWidth - deltaX newHeight = this.resizeStartHeight - deltaY newX = this.resizeStartLeft + deltaX newY = this.resizeStartTop + deltaY break case 'ne': newWidth = this.resizeStartWidth + deltaX newHeight = this.resizeStartHeight - deltaY newY = this.resizeStartTop + deltaY break case 'sw': newWidth = this.resizeStartWidth - deltaX newHeight = this.resizeStartHeight + deltaY newX = this.resizeStartLeft + deltaX break case 'se': newWidth = this.resizeStartWidth + deltaX newHeight = this.resizeStartHeight + deltaY break } // 应用最小尺寸限制 newWidth = Math.max(this.minWidth, newWidth) newHeight = Math.max(this.minHeight, newHeight) // 边界检测 if (newX < 0) { newWidth += newX newX = 0 } if (newY < 0) { newHeight += newY newY = 0 } if (newX + newWidth > window.innerWidth) { newWidth = window.innerWidth - newX } if (newY + newHeight > window.innerHeight) { newHeight = window.innerHeight - newY } this.width = newWidth this.height = newHeight this.x = newX this.y = newY }, // 鼠标释放处理 handleMouseUp() { this.isDragging = false this.isResizing = false this.resizeDirection = '' }, // 最小化窗口 minimize() { this.$emit('minimize') }, // 关闭窗口 close() { this.$emit('close') } } } </script>
<style scoped> .float-window { position: fixed; background: #fff; border: 1px solid #ddd; border-radius: 6px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); z-index: 9999; user-select: none; }
.window-header { height: 32px; background: #f5f5f5; border-bottom: 1px solid #ddd; border-radius: 6px 6px 0 0; display: flex; justify-content: space-between; align-items: center; padding: 0 12px; cursor: move; }
.window-title { font-size: 14px; font-weight: 500; color: #333; }
.window-controls { display: flex; gap: 4px; }
.control-btn { width: 20px; height: 20px; border: none; border-radius: 3px; background: #e0e0e0; cursor: pointer; font-size: 12px; display: flex; align-items: center; justify-content: center; }
.control-btn:hover { background: #d0d0d0; }
.window-content { padding: 12px; overflow: auto; height: calc(100% - 32px); }
/* 调节大小的控制点 */ .resize-handles { position: absolute; top: 0; left: 0; right: 0; bottom: 0; pointer-events: none; }
.resize-handle { position: absolute; pointer-events: auto; }
.resize-n { top: -3px; left: 3px; right: 3px; height: 6px; cursor: n-resize; }
.resize-s { bottom: -3px; left: 3px; right: 3px; height: 6px; cursor: s-resize; }
.resize-w { left: -3px; top: 3px; bottom: 3px; width: 6px; cursor: w-resize; }
.resize-e { right: -3px; top: 3px; bottom: 3px; width: 6px; cursor: e-resize; }
.resize-nw { top: -3px; left: -3px; width: 6px; height: 6px; cursor: nw-resize; }
.resize-ne { top: -3px; right: -3px; width: 6px; height: 6px; cursor: ne-resize; }
.resize-sw { bottom: -3px; left: -3px; width: 6px; height: 6px; cursor: sw-resize; }
.resize-se { bottom: -3px; right: -3px; width: 6px; height: 6px; cursor: se-resize; } </style>
|