js实现模态框的拖拽

61 篇文章 1 订阅

在这里插入图片描述效果图
1.css样式如下:

 <style>
        h2 {
            text-align: center;
            cursor: pointer;
        }

        .modal {
            width: 300px;
            height: 200px;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            background: skyblue;
            z-index: 1;
            display: none;
            cursor: move;
        }

        .modal span {
            position: absolute;
            right: 0;
            top: 0;
            display: block;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: pink;
            color: #fff;
            text-align: center;
            line-height: 50px;
            font-size: 24px;
            font-weight: bold;
            cursor: pointer;
        }

        .bg {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, .5);
            display: none;
        }
    </style>

在这里插入图片描述

!!!切记:modal的水平垂直居中千万不要使用此种方法
position:fixed;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
会有明显的bug

2.html+js 如下:

<body>
    <h2>点我显示模态框</h2>
    <div class="modal">
        <span>×</span>
    </div>
    <div class="bg"></div>
    <script>
        window.onload = function () {
            let modal = document.querySelector('.modal');
            let bg = document.querySelector('.bg');
            let h2 = document.querySelector('h2');
            let span  = document.querySelector('span');
            h2.addEventListener('click', function () {
                bg.style.display = 'block';
                modal.style.display = 'block';
            })

            span.addEventListener('click', function () {
                bg.style.display = 'none';
                modal.style.display = 'none';
            })

            modal.addEventListener('mousedown', function (e) {
                //获取鼠标在盒子内的坐标
                let x = e.pageX - modal.offsetLeft;
                let y = e.pageY - modal.offsetTop;
                function handleMove(e) {
                //模态框距离页面left和top值
                    let x2 = e.pageX - x;
                    let y2 = e.pageY - y;
                    modal.style.left = x2 + 'px';
                    modal.style.top = y2 + 'px';
                }
                document.addEventListener('mousemove', handleMove);
                document.addEventListener('mouseup', function () {
                    document.removeEventListener('mousemove', handleMove)
                })
            })
        }
    </script>
</body>

重点笔记:

1.点击弹出层,模态框和遮罩层就显示出来;
2.点击关闭按钮,模态框和遮罩层就会隐藏起来;
3.页面中拖拽原理:鼠标按下并移动,之后松开鼠标;
4.触发事件是鼠标按下mousedown,鼠标移动mousemove,鼠标弹起mouseup;
5.拖拽过程:鼠标移动过程中,获得最新的值赋给模态框的lefe和top值,这样模态框就可以跟住鼠标走了;
6.鼠标按下触发的事件源是整个模态框(根据自己需求而定);
7.鼠标的坐标减去鼠标在盒子内的坐标,才是模态框真正的位置。(!!!!!)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃葡萄不吐葡萄皮嘻嘻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值