b站pink老师JavaScript的PC端网页特效 案例代码——拖动模态框

本文档介绍了一个修复模态框在鼠标按下事件后,点击登录模态框内span导致意外移动的问题,以及鼠标放到span上不显示小手的错误。通过修改元素选择器和添加阻止冒泡的事件处理,实现了点击关闭按钮关闭模态框和背景,以及鼠标拖动标题区域可平滑移动模态框的功能。此外,还为span添加了鼠标样式,提高了用户体验。

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

【本代码相对pink老师原始代码,解决了之前设置mousedown鼠标按下事件之后,点击login模态框的子级span也可以移动模态框的BUG1和鼠标放到span上不变成小手,只有当点击a鼠标才变成小时的BUG2

BUG1解决方法:

(1)获取元素的时候,获取关闭模态框的元素选取span而非原始代码中的a

(2)JS中给login模态框的子级span加阻止冒泡e.stopPropagation();

因为eventTarget.addEventListener(type,listener[,useCapture])用事件监听方式注册事件的第三个值useCapture如果是true,则表明在捕获阶段,如果是false/不写(因为事件监听注册事件的第三个值默认是false)在则在冒泡阶段。

BUG2解决方法:

CSS中给span添加cursor: pointer;(鼠标放到span上变成小手)

目标效果:

1.点击“点击,弹出登录框”文字,弹出模态框和灰背景

且模态框在浏览器中水平+垂直居中(给模态框CSS设置固定/绝对定位,然后加上left:50%;   top:50%;   transform:translate(-50%,-50%);)

2.点击模态框中的关闭,即可关闭模态框和灰背景 

3.当模态框和灰背景显示的时候,当鼠标处在模态框的title区(即“登录会员”区域),可以鼠标移动拖动模态框在浏览器中移动,鼠标弹出(mouseup)的时候,模态框停止移动

注意:

e.pageX,   e.pageY,   e.offsetLeft,   e.offsetTop返回的值都没有单位,所以要用拼接字符串加上单位 

原理讲解:

讲解图以当模态框只是水平移动为例(模态框竖直移动,以及模态框水平和竖直同时移动原理相同)

位于上面的上图表示模态框没有移动的时候,位于下面图表示模态框在水平方向移动了

红点表示鼠标在模态框中title区域,点击拖动模态框移动的那个点

鼠标刚按下(mousedown)还未移动模态框的时候 x=(未移动模态框时的)e.pageX-login.offsetLeft

当模态框水平移动的时候,e.pageX的值改变,所以模态框在之前的基础上移动的距离=移动模态框后的e.pageX-鼠标刚按下(mousedown)还未移动模态框的时候 x

<!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>Document</title>
    <style>
        .login-header {
            width: 100%;
            text-align: center;
            height: 30px;
            font-size: 24px;
            line-height: 30px;
        }

        ul,
        li,
        ol,
        dl,
        dt,
        dd,
        div,
        p,
        span,
        h1,
        h2,
        h3,
        h4,
        h5,
        h6,
        a {
            padding: 0px;
            margin: 0px;
        }

        .login {
            display: none;
            /* 先给模态框设置隐藏 */
            position: fixed;
            /* 给模态框加一个固定定位 */
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            /* 以上三句话加上定位,实现login模态框在浏览器中水平和垂直居中 */
            background: #ffffff;
            box-shadow: 0px 0px 20px #ddd;
            z-index: 9999;
            border: #ebebeb solid 1px;
            width: 512px;
            height: 280px;
        }

        .login-title {
            width: 100%;
            margin: 10px 0px 0px 0px;
            text-align: center;
            line-height: 40px;
            height: 40px;
            font-size: 18px;
            position: relative;
            cursor: move;
        }

        .login-input-content {
            margin-top: 20px;
        }

        .login-button {
            width: 50%;
            margin: 30px auto 0px auto;
            line-height: 40px;
            font-size: 14px;
            border: #ebebeb 1px solid;
            text-align: center;
        }

        .login-bg {
            display: none;
            /* 先给模态框的背景也设置隐藏 */
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0px;
            left: 0px;
            background: rgba(0, 0, 0, .3);
        }

        a {
            text-decoration: none;
            color: #000000;
        }

        .login-button a {
            display: block;
        }

        .login-input input.list-input {
            float: left;
            line-height: 35px;
            height: 35px;
            width: 350px;
            border: #ebebeb 1px solid;
            text-indent: 5px;
        }

        .login-input {
            overflow: hidden;
            margin: 0px 0px 20px 0px;
        }

        .login-input label {
            float: left;
            width: 90px;
            padding-right: 10px;
            text-align: right;
            line-height: 35px;
            height: 35px;
            font-size: 14px;
        }

        .login-title span {
            position: absolute;
            font-size: 12px;
            right: -20px;
            top: -30px;
            background: #ffffff;
            border: #ebebeb solid 1px;
            width: 40px;
            height: 40px;
            border-radius: 20px;
            cursor: pointer;
            /* 当鼠标点击到login模态框中的span区域,鼠标变为小手 */
        }
    </style>
</head>

<body>
    <div class="login-header"><a id="link" href="javascript:;">点击,弹出登录框</a></div>
    <div id="login" class="login">
        <div id="title" class="login-title">登录会员
            <span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span>
        </div>
        <div class="login-input-content">
            <div class="login-input">
                <label>用户名:</label>
                <input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input">
            </div>
            <div class="login-input">
                <label>登录密码:</label>
                <input type="password" placeholder="请输入登录密码" name="info[password]" id="password" class="list-input">
            </div>
        </div>
        <div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div>
    </div>
    <!-- 遮盖层 -->
    <div id="bg" class="login-bg"></div>
    <script>
        // 1. 获取元素
        var login = document.querySelector('.login');
        var mask = document.querySelector('.login-bg');
        var link = document.querySelector('#link');
        var title = document.querySelector('#title');
        var span = document.querySelector('span');
        // 2. 点击弹出层这个链接 link,让模态框背景mask 和 模态框login 显示出来
        link.addEventListener('click', function () {
            mask.style.display = 'block';
            login.style.display = 'block';
        })
        // 3. 点击 span,隐藏 模态框背景mask 和 模态框login 
        span.addEventListener('click', function (e) {
            mask.style.display = 'none';
            login.style.display = 'none';
            e.stopPropagation();
            //解决添加了鼠标按下事件(mousedown)后,由于默认是冒泡,会导致点击login模态框的子级span也可以移动模态框的BUG
        })
        // 4. 开始拖拽
        // (1) 当鼠标在模态框中的title区域按下,获得鼠标在盒子内的坐标
        title.addEventListener('mousedown', function (e) {
            // (2) 鼠标移动的时候,把鼠标在页面中的坐标,减去 鼠标在盒子内的坐标就是模态框的left和top值
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            document.addEventListener('mousemove', move);
            // 只有按下(mousedown)后才能拖动鼠标(mousemove),所以mousemove要写在mousedown里面
            //在页面中任意地方都可以移动鼠标,所有用document绑定事件
            function move(e) {
                login.style.left = e.pageX - x + 'px';
                login.style.top = e.pageY - y + 'px';
                //注意:e.pageX,e.pageY,e.offsetLeft,e.offsetTop返回的值都没有单位,所以要用拼接字符串加上单位
            }
            // (3) 当鼠标在浏览器中任意地方弹起的时候(所以依然用document绑定事件),移除之前绑定的鼠标移动(mousemove)的事件(move)
            document.addEventListener('mouseup', function () {
                document.removeEventListener('mousemove', move);
            })
        })

    </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值