Web API---模态框拖拽

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .pop {
            display: block;
            margin: 0 auto;
        }

        .box {
            width: 200px;
            height: 200px;
            background-color: pink;
            display: none;
            position: absolute;
            left: 300px;
            top: 200px;
            z-index: 999;
        }

        h2 {
            text-align: center;
        }

        .del {
            background-color: red;
            width: 40px;
            height: 40px;
            position: absolute;
            border-radius: 50%;
            right: -20px;
            top: -20px;
            border: none;
        }

        .mask {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
            background-color: rgba(43, 42, 42, 0.1);
            display: none;
        }
    </style>
</head>

<body>
    <button class="pop">弹出模态框</button>
    <div class="box">
        <h2>标题</h2>
        <button class="del">删除</button>
    </div>
    <div class="mask"></div>

    <script>
        var pop = document.querySelector('.pop')
        var box = document.querySelector('.box')
        var del = document.querySelector('.del')
        var mask = document.querySelector('.mask')
        var title = document.querySelector('h2')
        // 1.点击弹出层,会弹出模态框, 并且显示灰色半透明的遮挡层。
        pop.onclick = function () {
            box.style.display = 'block';
            mask.style.display = 'block';
            // console.log(1);

        }

        // 2.点击关闭按钮,可以关闭模态框,并且同时关闭灰色半透明遮挡层。
        del.onclick = function () {
            box.style.display = 'none';
            mask.style.display = 'none';
            // console.log(del);
        }

        // 3.鼠标放到模态框最上面一行,可以按住鼠标拖拽模态框在页面中移动。
        title.onmousedown = function (e) {
            var x = e.pageX - box.offsetLeft;
            var y = e.pageY - box.offsetTop;
            document.onmousemove = function (e) {
                var boxLeft = e.pageX - x;
                var boxTop = e.pageY - y;
                box.style.left = boxLeft + 'px'
                box.style.top = boxTop + 'px'
            }
            // 4.鼠标松开,可以停止拖动模态框移动
            document.onmouseup = function () {
                document.onmousemove = null;
                console.log(12);
            }
        }




    </script>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值