【JS】案例1 拖动模态框

这篇博客详细介绍了如何使用HTML、CSS和JavaScript实现一个具有拖拽功能的模态登录框。通过监听mousedown、mousemove和mouseup事件,实现了模态框的拖动效果。模态框包含用户名和登录密码输入字段,并提供了关闭按钮。点击按钮可以显示和隐藏登录框,而拖动头部可以改变登录框的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
在这里插入图片描述

<!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;
            /* position: relative; */
        }
        .btn-login{
            margin-top: 30px;
            text-align: center;
            z-index: 100;
        }
        .mask{
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            /* background: #ccc; */
            background: rgba(0, 0, 0, 0.7);
            z-index: 1;
            /* overflow: auto; */
        }
        .login{
            display: none;
            position: absolute;
            width: 500px;
            height: 200px;
            border: 1px #ccc solid;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: white;
            z-index: 100;
        }
        .login-closeBtn{
            /* display: block; */
            width: 0;
            height: 0;
            position: absolute;
            top: -12px;
            right: -12px;
            text-align: center;
            border: 15px white solid;
            border-radius: 50%;
            box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.2);
        }
        .login-closeBtn a{
            display: inline-block;
            position: absolute;
            top: -10px;
            right: -20px;
            color: black;
            width: 40px;
            height: 40px;
            line-height: 20px;
            font-size: 10px;
            text-decoration: none;
            text-align: center;
        }
        .login-header{
            height: 40px;
            width: 100%;
            text-align: center;
            line-height: 40px;
            font-weight: 600;
        }
        .input-info{
            height: 40px;
            /* width: 200px; */
            padding: 10px 40px;
        }
        input{
            height: 25px;
            width: 300px;
            outline: none;
            /* display: inline; */
        }
        button{
            height: 35px;
            width: 200px;
            display: block;
            margin: auto;
            background-color: white;
            border: 1px #ccc solid;
        }
    </style>
</head>
<body>
    
    <div class="btn-login">点击,弹出登录框</div>
    
    <div class="login">
        <div class="login-closeBtn"><a href="javascript:;">关闭</a></div>
        <div class="login-header">登录会员</div>
        <div class="input-info">
            <label >用户名:</label>
            <input type="text" placeholder="请输入用户名">
        </div>
        <div class="input-info">
            <label>登录密码:</label>
            <input type="password" placeholder="请输入登录密码">
        </div>
        
        <button>登录会员</button>
    </div>
    <div class="mask"></div>
    <script>
        var login = document.querySelector('.login');
        var loginHeadr = document.querySelector('.login-header')
        var btnLogin = document.querySelector('.btn-login')
        var mask = document.querySelector('.mask')
        var closeBtn = document.querySelector('.login-closeBtn')

        btnLogin.onclick = function(){
            mask.style.display = 'block';
            login.style.display = 'block';
        }
        closeBtn.onclick = function(){
            mask.style.display = 'none';
            login.style.display = 'none';
        }
        //拖拽框实现
        //1.header事件触发:mousedown->mousemove->mouseup
        //2.mousedown:获取鼠标在模态框中坐标=鼠标在页面坐标-模块框在页面坐标
        //3.mousemove:模态框在页面坐标动态变化= 鼠标在页面坐标 - 鼠标在模态框中的坐标
        //4.mouseup:移除mousemove事件
        loginHeadr.addEventListener('mousedown', function(e){
            
            var x = e.pageX - login.offsetLeft
            var y = e.pageY - login.offsetTop;
            console.log(x, y);
            loginHeadr.addEventListener('mousemove', move)
            function move(e){
                login.style.left = e.pageX - x + 'px';
                login.style.top = e.pageY - y + 'px';           
            }
            loginHeadr.addEventListener('mouseup', function(){
                loginHeadr.removeEventListener('mousemove', move)
            })
        })
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值