mxs___ 2021-08-27 14:44 采纳率: 78.6%
浏览 30
已结题

鼠标按住关闭不放然后往任意地方松开后登录框会粘在鼠标上

鼠标按住关闭不放然后往任意地方松开后登录框会粘在鼠标上,请问如何解决,附上我的全部代码
img


<style>
        * {
            margin: 0;
            padding: 0;
        }
        /* body{
           
            text-align: center;
        } */
        a {
            display: block;
            font-size: 35px;
            width: 100%;
            text-align: center;
        }
        .touming {
            display: none;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,.3);
            position: fixed;
            top: 0;
            left: 0;
        }
        .box {
            display: none;
            position: absolute;
            left: 1px;
            top: 1px;
            width: 500px;
            height: 300px;
            background-color: #fff;
            z-index: 3;
            top: 50%;
            left: 50%;
           margin-top: -150px;
           margin-left: -250px;
            filter: drop-shadow(0 0 3px #333);
        }
        h1 {
            /* position: relative; */
            cursor: move;
        }
        h1 a {
            position: absolute;
            top: -43px;
            right: -23px;
            text-align: center;
            line-height: 50px;
            font-size: 14px;
            text-decoration: none;
            color: #000;
            width: 50px;
            height: 50px;
            border-radius: 100%;
            background-color: #fff;
        }
    </style>
</head>
<body>
    <a class="click" href="javascript:;">点击弹出登录框</a>
    <div class="box">
        <h1>登录会员<a class="close-btn" href="javascript:;">关闭</a></h1>
        <div>&emsp;用户名:<input type="text"></div>
        <div>登录密码:<input type="text"></div>
        <button>登录会员</button>
    </div>
    <div class="touming"></div>

    <script>
        var click = document.querySelector('.click');
        var touming = document.querySelector('.touming');
        var box = document.querySelector('.box');
        var h1 = document.querySelector('h1');
        click.addEventListener('click',function () {
            touming.style.display = 'block';
            box.style.display = 'block';
        })
        var close = document.querySelector('.close-btn');
        close.addEventListener('click',function () {
            touming.style.display = 'none';
            box.style.display = 'none';
            box.style.cssText = 'top:50%;left:50%'
        })
        h1.addEventListener('mousedown',function () {
            // console.log(box.offsetLeft);
            // console.log(event.pageX);
            var x = event.clientX - box.offsetLeft;
            var y = event.clientY - box.offsetTop;
            // console.log('鼠标在盒子中的距离是'+x+'鼠标在盒子中的距离是'+y);
             document.addEventListener('mousemove',move);
            function move() {
                console.log(event.clientX,event.clientY);
                console.log(x,y);
                box.style.left = event.clientX - x+250 +'px';
                box.style.top = event.clientY - y+150 +'px';
                console.log(box.style.left,box.style.top);
            };
           
            document.addEventListener('mouseup',function () {
                document.removeEventListener('mousemove',move)
            })
        })
        document.addEventListener('selectstart',function () {
            event.preventDefault();
        })
    </script>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • summerago 2021-08-27 17:42
    关注

    a换成button

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月12日
  • 已采纳回答 9月4日
  • 创建了问题 8月27日

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c