JavaScript 实现可拖动模态框的完整代码与原理

作者:闭包旅人

时间:2026-10-25

浏览:575

本文详细介绍如何使用 JavaScript 实现模态框的拖拽交互效果。通过监听鼠标事件获取坐标,计算偏移量并动态更新模态框位置,实现点击标题栏即可自由移动弹窗的功能。

文章目录

JavaScript 实现可拖动模态框的完整代码与原理

本文分享了一段使用 JavaScript 实现模态框拖拽效果的具体代码,供读者参考学习。

1. 实现效果:

JavaScript 实现可拖动模态框的完整代码与原理

点击链接后弹出模态框,点击关闭按钮即可关闭模态框。

在模态框的标题区域点击并拖动,可以随意改变模态框在页面上的位置。

实现该功能的核心在于准确获取鼠标的位置坐标。

2. 实现思路:

JavaScript 实现可拖动模态框的完整代码与原理

3. 代码实现:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖动模态框</title>
    <style>
        body {
            padding: 0;
            margin: 0;
        }
        .login {
            display: none;
            position: fixed;
            top: 50%;
            left:50%;
            box-sizing:border-box;
            width:400px;
            height: 200px;
            background-color: pink;
            box-shadow: 0px 0px 20px #ddd;
            z-index: 9999;
            transform: translate(-50%, -50%);
            /* 让一个固定定位的盒子 垂直居中 */
 
        
        }
        h4 {
            text-align: center;
            line-height: 70px;
        }
        form {
            text-align: center;
            margin-left: 40px;
        }
        a {
            text-decoration: none;
            text-align: center;
        }
        .finish {
            position: absolute;
            top:-15px;
            right:-15px;
            width: 30px;
            height: 30px;
            border: 1px solid white;
            border-radius: 50%;
            background-color: white;
            font-size: 10px;
            line-height: 30px;
        }
        .login-bg {
            display: none;
            width: 100%;
            height: 100%;
            position: fixed;
            top:0px;
            left: 0px;
            background-color: rgba(0,0,0,0.3);
        }
        .title {
            width: 400px;
            height: 50px;
            cursor: move;
            margin-top: -20px;
            height: 70px;
         
        }
    </style>
</head>
<body>
    <div class="bg">
        <div class="link"><a href="javascript:;" >点击,弹出登录框</a></div>
        <div class="login">
            <div class="finish">关闭</div>
            <div class = "title"><h4>登录会员</h4></div>
            <form action="">
                <table>
                    <tr>
                        <td>用户名:</td>
                        <td><input type="text" name="" id="" value="请输入用户名"></td>
                    </tr>
                    <tr>
                        <td>登录密码:</td>
                        <td><input type="text" name="" id="" value="请输入密码"></td>
                    </tr>
                </table>
                <input type="submit" name="" id="" value="登录会员">
            </form>
        </div>
    </div>
    <!-- 遮盖层 -->
    <div id="bg" class="login-bg"></div>
    <script>
        var a = document.querySelector('a');
        var login = document.querySelector('.login');
        var mask = document.querySelector('.login-bg');
        var finish = document.querySelector('.finish');
        a.addEventListener('click',function() {
            login.style.display = 'block';
            mask.style.display = 'block';
        });
        finish.addEventListener('click', function(){
            login.style.display = 'none';
            mask.style.display = 'none';
        })
        var title = document.querySelector('.title');
        title.addEventListener('mousedown', function(e){
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            // 鼠标移动的时候,把鼠标在页面中的坐标,减去 鼠标在盒子内的坐标就是模态框的left和top值。
            document.addEventListener('mousemove', move)
            function move(e) {
                // 别忘了加单位。
                login.style.left = e.pageX - x + 'px';
                login.style.top = e.pageY - y + 'px';
            }
            // 鼠标弹起,让鼠标移动事件停止。
            document.addEventListener('mouseup', function() {
                document.removeEventListener('mousemove', move);
            })
        })
    </script>
</body>
</html>

以上就是本文的全部内容,希望这段代码能对各位的学习有所帮助,同时也欢迎大家继续支持本站。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章