在vue2的项目中实现窗口的拖动效果
<div class="notification" ref="notification" v-if="modalVisible" @mousedown="startDrag"></div>
我写的这个 点击后窗口显示出来 我当时实现的是显示的窗口可以进行拖动的效果
// 拖拽功能
startDrag(event) {
this.notification = this.$refs.notification; // 获取弹窗元素的引用
this.startX = event.clientX;
this.startY = event.clientY;
this.offsetX = this.notification.offsetLeft;
this.offsetY = this.notification.offsetTop;
document.addEventListener('mousemove', this.dragging);
document.addEventListener('mouseup', this.stopDrag);
},
dragging(event) {
const offsetX = event.clientX - this.startX;
const offsetY = event.clientY - this.startY;
this.notification.style.left = this.offsetX + offsetX + 'px';
this.notification.style.top = this.offsetY + offsetY + 'px';
},
stopDrag() {
document.removeEventListener('mousemove', this.dragging);
document.removeEventListener('mouseup', this.stopDrag);
},