用js实现模态框效果

难点:

  1. 隐藏和现实各个盒子
  2. 为具体盒子绑定具体事件
  3. 在鼠标拖动时登录框对浏览器的距离和鼠标离开时的距离 

 最终效果

 

 

 

 


<!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>
        * {
            padding: 0;
            margin: 0;
        }
        a {
            text-decoration: none;
            color: black;
        }
        .login {
            text-align: center;
        }
        .login  a {
               font-size: 30px;
               font-weight: 20px;
               color: black;     
               
        }
        .backG {
            display: none;
            background-color: rgba(0,0,0, .3);
           width: 100%;
           height: 100%;
           position: fixed;
           top: 0;
           left: 0;

        }
        .login-denglu {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            width: 500px;
            height: 350px;
            background-color: white;
            border: 1px solid #ccc;
            box-shadow: 0px 0px 20px black;
            transform: translate(-50%,-50%);
            z-index: 999;
        }
        .zhongxin{
                width: 100%;
                text-align: center;
                cursor: move;
            }
        .login-denglu .tuichu {
            position: absolute;
            top: -25px;
            right: -25px;
            width: 50px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            border-radius: 50px;
            box-shadow: 0px 0px 10px black;
            background-color: aliceblue;
        }
        input {
            outline: none;
            width: 270px;
            height: 30px;
            font-size: 20px;
            border: 1px solid #ccc;
        }
        .gonggong {
            width: 100%;
            margin-top: 60px;
        }
        label {
            display: inline-block;
            width: 100px;
            text-align: justify;
            text-align: center;
            margin-right: 10px;
            font-size: 20px; 
        }
        .dengluhuiyuan {
            width: 100%;
            margin-top: 40px;
            text-align: center;
        }
        button {
            width: 200px;
            height: 30px;
            font-size: 20px;
            border: 1px solid #ccc;
            text-align: center;
            cursor: pointer;
            background-color: white;
        }
    </style>
</head>
<body>
    <div class="login"><a href="javascript:;" id="aone">点击,弹出登录框</a></div>
    <div class="login-denglu">
        <div class="tuichu"><a href="javascript:;" id="atwo">退出</a></div> 
        <div class="zhongxin"><h1>会员中心</h1></div>
        <div class="yonghu gonggong" ><label for="">用户名:</label><input type="text" placeholder="请输入用户名"></div>
        <div class="mima gonggong"><label for="">密码: </label><input type="password" placeholder="请输入密码"></div>
        <div class="dengluhuiyuan"><button>确认登录</button></div>
    </div>
    <div class="backG"></div>
    <script>
        var aone=document.querySelector('#aone');
        var logindenglu=document.querySelector('.login-denglu');
        var backG=document.querySelector('.backG');
        var atwo=document.querySelector('#atwo');
        var zhongxin=document.querySelector('.zhongxin');
        //隐藏和出现遮蔽层和登录界面
        aone.addEventListener('click',function () {
            backG.style.display='block';
            logindenglu.style.display='block';
        })
        atwo.addEventListener('click',function () {
            backG.style.display='none';
            logindenglu.style.display='none';
        })


       zhongxin.addEventListener('mousedown',function (e) {
        //求出鼠标距离登录框左和上固定距离
           var x=e.pageX-logindenglu.offsetLeft;
           var y=e.pageY-logindenglu.offsetTop;
           
          document.addEventListener('mousemove',move)
          //用到移除 所以将方法写在外面
          function move (e) {
              logindenglu.style.left=e.pageX-x+'px';//e.page求出现在鼠标距页面距离减去固定距离就为登录界面现在距离
              logindenglu.style.top=e.pageY-y+'px';//e.page求出现在鼠标距页面距离减去固定距离就为登录界面现在距离
          }
          document.addEventListener('mouseup',function () {
              document.removeEventListener('mousemove',move)//移除事件
          })
        })

      
    </script>
</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值