JavaScript拖动元素位置示例

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style>
        ul,li,ol{
            list-style: none;
            margin: 0;
            padding: 0;
        }
        .main{
            width: 500px;
            margin: 0 auto;
            position: relative;
            height: 500px;
        }
        .loogin{
            width: 390px;
            height: 300px;
            background-color: white;
            border: 1px solid #ccc;
            display: none;
            position: relative;
            top: 0;
            left: 0;
        }
        .loogin input{
            width: 300px;
            height: 30px;
            border: 1px solid #999;
            border-radius: 100px;
            outline: none;
            
        }
        .pwd{
            margin-left: 10px;
            margin-bottom: 20px;
        }
        .mimas{
            display: flex;
            justify-content: space-between;
            margin-bottom: 50px;
            padding: 0 10px;
        }
        .username{
            margin-bottom: 20px;
            
        }
        .submit{
            height: 30px;
            width: 200px;
            border: none;
            border-radius: 50px;
            display: block;
            margin: 0 auto;
        }
        .title{
            height: 50px;
            border-bottom: 1px dotted #999;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            cursor: move;
        }
        .title .close{
            margin-right: 5px;
            width: 20px;
            height: 20px;
            border: 1px solid #999;
            text-align: center;
            margin-top: 5px;
            cursor: pointer;
        }
    </style>
    <div class="main">
        <div class="btn">点击登录</div>
    <div class="loogin">
        <div class="title">
            <div class="title_info"></div>
            <div class="close">X</div>
        </div>
        <form action="">
           <div class="username">
               <label for="">用户名:</label>
               <input type="text" name="" id="">
           </div>
           <div class="pwd">
               <label for="passwodrd">密 码:</label>
               <input type="password">
           </div>
           <ul class="mimas">
               <li>记住我</li>
               <li>忘记密码?</li>
           </ul>
           <button class="submit">登录</button>
        </form>
    </div>
    </div>
    <script>
        var btn = document.querySelector('.btn')
        var login = document.querySelector('.loogin')
        var close = document.querySelector('.close')
        var title = document.querySelector('.title')
        btn.addEventListener('click',function(){
            login.style.display = 'block'
            this.style.display = 'none'
            document.body.style.backgroundColor = '#ccc'
        })
        close.addEventListener('click',function(){
            login.style.display = 'none'
            location.reload()
        })
        title.addEventListener('mousedown',function(e){
            var x = e.pageX - login.offsetLeft
            var 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>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值