拖动登录模拟框

拖动登录模拟框

效果展示:
在这里插入图片描述在这里插入图片描述代码实现

<!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>
        *{
            background-color:#ccc;
        }
        .dl{
            text-align: center;
            font-size: 25px;
        }
        .box{
            width: 300px;
            height: 200px;
            border:2px solid rgb(85, 9, 9);
            margin-top: 50px;
            text-align: center;
            margin-left: 50%;
            transform: translateX(-50%);
            border-radius: 5px;
            position: absolute;

            display: none;
        }
        .box h3{
            width: 100%;
        }
        p{
            text-align: center;
        }
        .container{
            margin-top: 30px;
        }
        .close{
            position: relative;
            top: -180px;
            left: 305px;
            width:30px;
            height: 30px;
            border: 1px solid rgb(85, 9, 9);
            border-radius: 100%;
            font-size: 10px;
            line-height: 30px;
        }
    </style>
</head>
<body>
    <div class="dl">点击弹出登录框</div>

    <div class="box">
        <h3>登录会员</h3>
        <div class="container">
            用户姓名:<input type="text">
            <br>
            用户密码:<input type="text">
            <p>登录会员</p>
        </div>
        <div class="close">关掉</div>
    </div>
    <script>
        //获得元素源
        var dl = document.querySelector('.dl');
        var box = document.querySelector('.box');
        var h3 = document.querySelector('h3');
        var container = document.querySelector('.container');
        var close = document.querySelector('.close');

        //点击弹出登录框
        dl.addEventListener('click',function (e) {
            box.style.display = 'block';
            //点击关掉登录框
            close.addEventListener('click',function (params) {
                box.style.display = 'none';
            })
        });
        //鼠标点击title,按着移动登录框跟着移动
        box.addEventListener('mousedown',function (e) { //鼠标距离内框的距离
                var x = e.pageX - h3.offsetLeft;
                var y = e.pageY - h3.offsetTop;
                document.addEventListener('mousemove',fn);
                //给盒子重新赋值左边距和右边距
                function fn(e) {
                    box.style.left = e.pageX - x + 'px';
                    box.style.top = e.pageY - y + 'px';
                }
                //当鼠标不在按着,清除盒子跟随鼠标移动事件
                document.addEventListener('mouseup',function (params) {
                    document.removeEventListener('mousemove',fn);
                })
            })
    </script>
</body>
</html>

注意事项
1,关于鼠标的一些事件的复习:
mousedown: 鼠标按下保持触发;
mouseup: 鼠标按下松开触发;
mousemove: 鼠标光标移动促触发;

2,关于这些事件之间一些逻辑注意:
鼠标按下触发事件,鼠标在运送时,获取鼠标所在光标的位置,鼠标松开,清除的登录框随着光标移动事件。

document.removeEventListener('mousemove',fn);

3布局实现注意。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值