Vue达成el-dialog聊天框拖拽位子、放大显示

原创
小哥 3年前 (2022-11-10) 阅读数 101 #大杂烩

1.将属性添加到已写入的对话框

:close-on-click-modal="false"//不要单击遮罩层以关闭弹出层
 v-dialogDrag//该指令可以实现弹出窗口的全屏和拉伸。
 //如果是form表单,不放置提交和其他按钮el-form-item为了避免在上下伸展时被隐藏。

下面是添加的位置,可以模仿


        
        
      

2.准备实施功能js文件----------drag.js

// v-dialogDrag: 弹窗拖拽+水平伸缩式
import Vue from vue;
Vue.directive(dialogDrag, {
    bind(el, binding, vnode, oldVnode) {
        // 子弹架可以拉伸最小宽度和高度。
        const minWidth = 400
        const minHeight = 300
            // 初始非全屏
        let isFullScreen = false
            // 当前顶部高度
        let nowMarginTop = 0
            // 获取弹出框的头部(在此部分中双击全屏)
        const dialogHeaderEl = el.querySelector(.el-dialog__header)
            // 弹窗
        const dragDom = el.querySelector(.el-dialog)
            // 添加到弹出窗口overflow auto; 否则,框中的标签可能超过dialog;
        dragDom.style.overflow = auto
            // 清除选择标题文本效果
            // dialogHeaderEl.onselectstart = new Function("return false");
            // 头部加可拖动cursor
        dialogHeaderEl.style.cursor = move
            // 获取原始属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
        const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
        const moveDown = 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
            }
        }
        dialogHeaderEl.onmousedown = moveDown
            // 当前宽高
        let nowWidth = 0
            // let nowHight = 0
            // 双击头部全屏效果
        dialogHeaderEl.ondblclick = e => {
            if (isFullScreen === false) {
                // nowHight = dragDom.clientHeight
                nowWidth = dragDom.clientWidth
                nowMarginTop = dragDom.style.marginTop
                dragDom.style.left = 0
                dragDom.style.top = 0
                dragDom.style.height = 100VH
                dragDom.style.width = 100VW
                dragDom.style.marginTop = 0
                isFullScreen = true
                dialogHeaderEl.style.cursor = initial
                dialogHeaderEl.onmousedown = null
            } else {
                dragDom.style.height = auto
                dragDom.style.width = nowWidth + px
                dragDom.style.marginTop = nowMarginTop
                isFullScreen = false
                dialogHeaderEl.style.cursor = move
                dialogHeaderEl.onmousedown = moveDown
            }
        }
        dragDom.onmousemove = function(e) {
            // let moveE = e
            if (
                e.clientX > dragDom.offsetLeft + dragDom.clientWidth - 10 ||
                dragDom.offsetLeft + 10 > e.clientX
            ) {
                dragDom.style.cursor = w-resize
            } else if (
                el.scrollTop + e.clientY >
                dragDom.offsetTop + dragDom.clientHeight - 10
            ) {
                dragDom.style.cursor = s-resize
            } else {
                dragDom.style.cursor = default

                dragDom.onmousedown = null
            }
            dragDom.onmousedown = e => {
                const clientX = e.clientX
                const clientY = e.clientY
                const elW = dragDom.clientWidth
                const elH = dragDom.clientHeight
                const EloffsetLeft = dragDom.offsetLeft
                const EloffsetTop = dragDom.offsetTop
                dragDom.style.userSelect = none
                const ELscrollTop = el.scrollTop
                    // 确定点击的位置是否为头部
                if (
                    clientX > EloffsetLeft &&
                    clientX < EloffsetLeft + elW &&
                    clientY > EloffsetTop &&
                    clientY < EloffsetTop + 100
                ) {
                    // 如果头部在这里不做任何动作,则上面的dialogHeaderEl.onmousedown = moveDown;
                } else {
                    document.onmousemove = function(e) {
                            // 移动时禁用默认事件
                            e.preventDefault()
                                // 鼠标左键拖动位置
                            if (clientX > EloffsetLeft && clientX < EloffsetLeft + 10) {
                                // 往左拖拽
                                if (clientX > e.clientX) {
                                    dragDom.style.width = elW + (clientX - e.clientX) * 2 + px
                                }
                                // 往右拖拽
                                if (clientX < e.clientX) {
                                    if (dragDom.clientWidth < minWidth) {
                                        console.log()
                                    } else {
                                        dragDom.style.width = elW - (e.clientX - clientX) * 2 + px
                                    }
                                }
                            }
                            // 鼠标右键拖动位置
                            if (
                                clientX > EloffsetLeft + elW - 10 &&
                                clientX < EloffsetLeft + elW
                            ) {
                                // 往左拖拽
                                if (clientX > e.clientX) {
                                    if (dragDom.clientWidth < minWidth) {
                                        console.log()
                                    } else {
                                        dragDom.style.width = elW - (clientX - e.clientX) * 2 + px
                                    }
                                }
                                // 往右拖拽
                                if (clientX < e.clientX) {
                                    dragDom.style.width = elW + (e.clientX - clientX) * 2 + px
                                }
                            }
                            // 鼠标底部拖动位置
                            if (
                                ELscrollTop + clientY > EloffsetTop + elH - 20 &&
                                ELscrollTop + clientY < EloffsetTop + elH
                            ) {
                                // 往上拖拽
                                if (clientY > e.clientY) {
                                    if (dragDom.clientHeight < minHeight) {
                                        console.log()
                                    } else {
                                        dragDom.style.height = elH - (clientY - e.clientY) * 2 + px
                                    }
                                }
                                // 往下拖拽
                                if (clientY < e.clientY) {
                                    dragDom.style.height = elH + (e.clientY - clientY) * 2 + px
                                }
                            }
                        }
                        // 拉伸结束
                    document.onmouseup = function(e) {
                        document.onmousemove = null

                        document.onmouseup = null
                    }
                }
            }
        }
    }
})

3.将写好的drag.js文件引入main.js


请注意,可以根据自己的地址更改地址

import ./utils/drag
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除