antdVue中a-modal的拖拽移动功能

在utils中新建drag.js文件

import Vue from 'vue'

// v-dialogDrag: 弹窗拖拽
Vue.directive('dragModal', {
    bind(el, binding, vnode, oldVnode) {
    	// 如果要给其他UI框架中添加modal拖拽事件 修改此处即可
        const dialogHeaderEl = el.querySelector('.ant-modal-header')
        const dragDom = el.querySelector('.ant-modal')
        
        dialogHeaderEl.style.cursor = 'move'
        // 获取原有属性 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}px`
                // 将此时的位置传出去
                // binding.value({x:e.pageX,y:e.pageY})
            }
            document.onmouseup = function (e) {
                document.onmousemove = null
                document.onmouseup = null
            }
        }
    }
})

在main.js文件中引入

import '@/utils/drag'

使用到实例中:v-dragModalt

 <a-modal v-model:visible="visible" title="Basic Modal" @ok="handleOk"  v-dragModalt></a-modal>
Ant Design Vue (AntdVue) 的 `a-modal` 是一个轻量级的弹窗组件,常用于创建模态框、对话框或通知。它提供了一些基本的功能,如显示和关闭模态内容,配置标题、大小、位置等。以下是 `a-modal` 主要的 API 属性和方法: 1. **props** (属性): - `visible`: 控制模态是否可见,默认值为 `false`。布尔型,可通过 `this.$refs.modal.show()` 或者通过绑定的事件改变。 - `title`: 模态窗口的标题,字符串类型。 - `width` 和 `height`: 模态窗口的尺寸,可以设置为固定像素值或百分比。 - `center` 或 `fullscreen`: 是否居显示或全屏模式。 - `mask`: 是否显示遮罩层,默认 true。 - `maskClosable`: 是否允许点击遮罩层关闭模态,默认 true。 2. **methods** (方法): - `open()`: 显示模态。 - `close()`: 关闭模态。 - `onOk(event)`: 确定按钮或自定义确认操作触发的回调函数,event 参数是点击事件。 - `onCancel(event)`: 取消按钮或自定义取消操作触发的回调函数,event 参数同上。 3. **events** (事件): - `onVisibleChange(visible)`: 当模态的可见状态变化时触发,visible 是新的可见状态。 - `onAfterClose()`: 模态关闭后触发。 要使用 `a-modal`,通常会在模板引用组件,并通过 props 和 methods 进行配置。例如: ```html <template> <a-modal v-model="modalVisible" :title="modalTitle"> <div slot="content">这里是模态的内容</div> <a-button type="primary" @click="handleOk">确定</a-button> <a-button type="ghost" @click="handleCancel">取消</a-button> </a-modal> </template> <script> export default { data() { return { modalVisible: false, modalTitle: '这是一个模态' }; }, methods: { handleOk() { this.$refs.modal.close(); // 关闭模态并调用回调 }, handleClose() { this.modalVisible = false; } } }; </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值