对话框拖拽指令

        Vue.directive('dialogDrag', {
            bind (el, binding, vnode, oldVnode) {
                const dialogHeaderEl = el.querySelector('.el-dialog__header')
                const dragDom = el.querySelector('.el-dialog')
                dialogHeaderEl.style.cursor = 'move'
                // console.log("绑定元素:",el);
                // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
                const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)

                dialogHeaderEl.onmousedown = (e) => {
                    // 鼠标按下,计算当前元素距离可视区的距离
                    const disX = e.clientX - dialogHeaderEl.offsetLeft
                    const disY = e.clientY - dialogHeaderEl.offsetTop

                    // 获取到的值带px 正则匹配替换
                    let styL, styT

                    // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
                    if (sty.left.includes('%')) {
                        styL = +document.body.clientWidth * (+sty.left.replace(/%/g, '') / 100)
                        styT = +document.body.clientHeight * (+sty.top.replace(/%/g, '') / 100)
                    } else {
                        styL = +sty.left.replace(/px/g, '')
                        styT = +sty.top.replace(/px/g, '')
                    }

                    document.onmousemove = function (e) {
                        // 通过事件委托,计算移动的距离
                        const l = e.clientX - disX
                        const t = e.clientY - disY

                        // 移动当前元素
                        dragDom.style.left = `${l + styL}px`
                        // 判断弹窗位置,防止弹窗头部移出可视区
                        dragDom.style.top = `${(t + styT) < 0 ? 0 : t + styT}px`
                    }

                    document.onmouseup = function (e) {
                        document.onmousemove = null
                        document.onmouseup = null
                    }
                }
            }
        });

### 创建可拖动的 Notify 对话框 为了在 Vue 2 中创建一个可以拖动的通知 (notify) 对话框,可以通过结合 CSS 和 JavaScript 实现这一功能。下面是一个详细的实现方法: #### 安装依赖库 首先安装 `vue-notification` 库用于处理通知逻辑。 ```bash npm install vue-notifications --save ``` #### 注册全局组件 接着,在项目的入口文件 main.js 或者相应的配置文件中注册该插件。 ```javascript import Notifications from 'vue-notification' Vue.use(Notifications); ``` #### 编写 HTML 结构与样式 定义好对话框的基础结构以及必要的样式属性以便支持拖拽操作。 ```html <template> <div class="draggable-notify"> <!-- Dialog container --> <transition name="fade"> <notification-group group="foo"> <div style="position:relative;"> <notification v-for="(item, index) in items" :key="index" @click.native="handleClick(item)" :style="{ top: item.top + 'px', left: item.left + 'px' }" class="drag-area"> {{ item.text }} </notification> </div> </notification-group> </transition> </div> </template> <style scoped> .draggable-notify { position: fixed; bottom: 0; right: 0; } .notification { background-color: white; border-radius: 8px; box-shadow: 0 3px 6px rgba(0, 0, 0, .16); cursor: move; } /* 设置拖动手柄 */ .drag-area{ width: 100%; height: 100%; touch-action:none; /* 支持触摸屏设备上的平移手势 */ } .fade-enter-active, .fade-leave-active { transition: opacity 0.5s ease-out; } .fade-enter, .fade-leave-to { opacity: 0; } </style> ``` #### 添加交互逻辑 利用 Vue 的内置指令和事件监听机制来捕捉用户的鼠标动作,并据此调整元素的位置。 ```javascript <script> export default { data() { return { dragging: false, currentX: null, currentY: null, initialX: null, initialY: null, items: [ { text: "这是一条消息", top: 20, left: 20 } ] }; }, methods: { handleClick(notification){ this.$notify({ group: 'foo', title: notification.text, type: 'warn' }) }, handleMouseDown(event){ let target = event.target.closest('.drag-area'); if (!target || !this.items.length) return; const rect = target.getBoundingClientRect(); this.currentX = event.clientX; this.currentY = event.clientY; this.initialX = rect.x - this.currentX; this.initialY = rect.y - this.currentY; document.addEventListener('mousemove', this.handleMouseMove); document.addEventListener('mouseup', this.handleMouseUp); this.dragging = true; }, handleMouseMove(event){ if(!this.dragging){return;} this.currentX = event.clientX; this.currentY = event.clientY; this.items.forEach((item)=>{ item.top = this.currentY + this.initialY; item.left = this.currentX + this.initialX; }); }, handleMouseUp(){ this.dragging=false; document.removeEventListener('mousemove', this.handleMouseMove); document.removeEventListener('mouseup', this.handleMouseUp); } }, mounted(){ window.document.querySelector(".draggable-notify").addEventListener("mousedown", this.handleMouseDown); }, beforeDestroy(){ window.document.querySelector(".draggable-notify").removeEventListener("mousedown", this.handleMouseDown); } }; </script> ``` 上述代码展示了如何构建一个简单的、能够响应鼠标的按下、移动和释放事件来进行位置更新的消息弹窗[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值