StXch 2022-11-21 20:17 采纳率: 0%
浏览 8
已结题

鼠标点击div移动时,div自己向下移动一段距离

鼠标点击div移动时,div自己向下移动距离
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            width: 100%;
            height: 100%;
        }

        h1 {
            font-weight: 400;
            text-align: center;
            cursor: pointer;
        }

        .zeDan {
            display: none;
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0;
            left: 0;
            background-color: rgba(0, 0, 0, .3);
        }

        .login {
            display: none;
            position: relative;
            top: 50%;
            left: 50%;
            transform: translate(-50%, 50%);
            width: 540px;
            height: 330px;
            background-color: #fff;
        }

        .login .title {
            height: 70px;
            padding-top: 40px;
            text-align: center;
            font-weight: 400;
            font-size: 22px;
        }

        .login form {
            margin-top: 22px;
            padding-left: 30px;
            width: 490px;
            text-align: right;
        }

        .login form .input01 {
            margin-top: 20px;
            margin-left: 5px;
            width: 320px;
            height: 40px;
            outline: none;
            border: none;
        }

        .login form #loginTab {
            width: 200px;
            height: 40px;
            margin-top: 30px;
            margin-right: 120px;
        }

        .login span {
            position: absolute;
            top: -25px;
            right: -25px;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            text-align: center;
            line-height: 50px;
            background-color: #eee;
            cursor: pointer;
        }

    </style>
    <script>
        window.addEventListener('load', function () {
            let h1 = document.querySelector('.h1');
            let zeDan = document.querySelector('.zeDan');
            let login = document.querySelector('.login');
            let span  = document.querySelector('span');
            let title = document.querySelector('.title');
            h1.addEventListener('click', function () {
                zeDan.style.display = 'block';
                login.style.display = 'block';
            });
            span.addEventListener('click', function () {
                zeDan.style.display = 'none';
                login.style.display = 'none';
            });
            title.addEventListener('mousedown', function (e) {
                let x = e.pageX - login.offsetLeft;
                let y = e.pageY - login.offsetTop;
                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>
</head>
<body>
<h1 class="h1">点击,弹出登录框</h1>
<div class="zeDan"></div>
<div class="login">
    <div class="title">登录会员</div>
    <form action="javascript:;">
        用户名: <input type="text" class="input01"><br>
        登录密码: <input type="password" class="input01">
        <input type="submit" value="登录会员" id="loginTab">
    </form>
    <span>关闭</span>
</div>
</body>
</html>

点击title时会向下弹一段距离,如何解决,谢谢~
  • 写回答

2条回答 默认 最新

  • 这个需求做不了123 2022-11-21 21:45
    关注

    你点击的时候有的元素显示有的隐藏,隐藏的元素不占页面页面空间,导致页面跳动
    有个css面试题是这样,问display:none;和visibility:hidden;的区别是什么?你搞清这个,那你就能解决你现在遇到的这个问题了

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月7日
  • 创建了问题 11月21日

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊