js 弹窗拖动

<!DOCTYPE html>
<html>

<head>
    <title>测试</title>
    <style>
        html {
            font-size: 14px;
        }

        html,
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            width: 100vw;
        }


        .dialog {
            top: 0;
            left: 0;
            position: fixed;
            display: none;
            width: 100vw;
            height: 100vh;
            z-index: 999;
        }

        .mask {
            background-color: rgba(0, 0, 0, 0.3);
            height: 100%;
            width: 100%;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .d_container {
            background-color: #fff;
            width: 60%;
            height: 50%;
            position: absolute;
        }

        .d_header {
            background-color: #f2f4f7;
            height: 3.5rem;
            display: flex;
        }

        .drag {
            height: 100%;
            width: calc(100% - 3.5rem);
            display: flex;
            align-items: center;
            padding: 0 2rem;
            box-sizing: border-box;
            cursor: move;
          user-select: none;
        }

        .close {
            height: 100%;
            width: 3.5rem;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .close:hover {
            background-color: red;
            color: #fff;
        }

        .d_content {
            height: calc(100% - 3.5rem);
            width: 100%;
        }
    </style>
    <script>
        window.onload = function () {
            drag()
        }
        function drag() {
            let list = document.querySelector('.drag')
            let box1 = document.querySelector('.drag_box')
            list.onmousedown = function (e) {//点击的位置   移动的距离   移动距离
                let boxleft = box1.getBoundingClientRect().left
                let boxtop = box1.getBoundingClientRect().top
                let x = e.x
                let y = e.y
                document.onmousemove = function (d) {
                    box1.style.left = boxleft + d.x - x + 'px'
                    box1.style.top = boxtop + d.y - y + 'px'
                }
                document.onmouseup = function () {
                    document.onmousemove = null;
                }
            }
        }
        /* 关闭弹窗 */
        function closeDialog(e) {
            e.stopPropagation();
            let dialog = document.querySelector('.dialog')
            dialog.style.display = "none"
        }
        /* 开启弹窗 */
        function openDialog() {
            let dialog = document.querySelector('.dialog')
            dialog.style.display = "block"
        }
    </script>
</head>

<body>
    <div class="container">
        <button onclick="openDialog()">打开弹窗</button>
        <div class="dialog">
            <div class="mask">
                <div class="d_container drag_box">
                    <div class="d_header">
                        <div class="drag">
                            <div class="d_h_title">
                                详情
                            </div>
                        </div>
                        <div onclick="closeDialog(event)" class="close">X</div>
                    </div>
                    <div class="d_content">
                        <table>
                            <thead></thead>
                            <tbody></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中,可以通过自定义指令实现弹窗的拖拽和拉伸功能。 首先,对于拖拽功能,可以创建一个自定义指令`v-draggable`。在该指令的绑定元素上设置`mousedown`事件监听器,当鼠标按下时触发拖拽操作。在`mousemove`和`mouseup`事件监听器中处理鼠标移动和释放的逻辑。具体代码如下: ```javascript Vue.directive('draggable', { bind(el) { el.style.position = 'absolute'; el.addEventListener('mousedown', startDrag); function startDrag(e) { const startX = e.clientX; const startY = e.clientY; const initialX = el.offsetLeft; const initialY = el.offsetTop; document.addEventListener('mousemove', dragElement); document.addEventListener('mouseup', releaseElement); function dragElement(e) { el.style.left = `${initialX + (e.clientX - startX)}px`; el.style.top = `${initialY + (e.clientY - startY)}px`; } function releaseElement() { document.removeEventListener('mousemove', dragElement); document.removeEventListener('mouseup', releaseElement); } } } }); ``` 接下来,对于拉伸功能,可以创建一个自定义指令`v-resizable`。在该指令的绑定元素上设置`mousedown`事件监听器,当鼠标按下时触发拉伸操作。在`mousemove`和`mouseup`事件监听器中处理鼠标移动和释放的逻辑。具体代码如下: ```javascript Vue.directive('resizable', { bind(el) { el.style.position = 'absolute'; el.style.resize = 'both'; el.addEventListener('mousedown', startResize); function startResize(e) { const startX = e.clientX; const startY = e.clientY; const initialWidth = el.offsetWidth; const initialHeight = el.offsetHeight; document.addEventListener('mousemove', resizeElement); document.addEventListener('mouseup', releaseElement); function resizeElement(e) { el.style.width = `${initialWidth + (e.clientX - startX)}px`; el.style.height = `${initialHeight + (e.clientY - startY)}px`; } function releaseElement() { document.removeEventListener('mousemove', resizeElement); document.removeEventListener('mouseup', releaseElement); } } } }); ``` 最后,在需要拖拽和拉伸的弹窗组件上使用`v-draggable`和`v-resizable`指令即可实现相应功能。例如: ```html <template> <div class="modal" v-draggable v-resizable> <!-- 弹窗内容 --> </div> </template> ``` 通过上述自定义指令的方式,我们可以在Vue中实现弹窗的拖拽和拉伸功能,提升用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值