登录框拖拽JS 和JQ实现

3 篇文章 0 订阅

js

<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .login_header {
            text-align: center;
        }

        a {
            text-decoration: none;
            color: #000;
        }

        .login {
            display: none;
            width: 512px;
            height: 280px;
            position: fixed;
            border: #ebebeb solid 1px;
            left: 50%;
            top: 50%;
            background-color: #fff;
            box-shadow: 0 0 20px #ddd;
            z-index: 9999;
            transform: translate(-50%, -50%);
        }

        .login_title {
            width: 100%;
            margin: 10px 0 0 0;
            text-align: center;
            line-height: 40px;
            height: 40px;
            font-weight: 800;
            position: relative;
            cursor: move;
        }

        .login_input_content {
            margin-top: 20px;
        }

        .login_button {
            width: 50%;
            margin: 30px auto 0 auto;
            line-height: 40px;
            font-size: 14px;
            border: 1px solid #ebebeb;
            text-align: center;
        }

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

        .login_button a {
            display: block;
        }

        .login_input input {
            float: left;
            line-height: 35px;
            width: 350px;
            height: 35px;
            border: 1px solid #ebebeb;
            text-indent: 5px;
            outline: none;
        }

        .login_input {
            overflow: hidden;
            margin: 0 0 20px 0;
        }

        .login_input label {
            float: left;
            width: 90px;
            padding: 10px;
            text-align: right;
            line-height: 35px;
            height: 35px;
            font-size: 14px;
        }

        .login_title span {
            position: absolute;
            font-size: 12px;
            right: -20px;
            top: -30px;
            background-color: #fff;
            border: 1px solid #ebebeb;
            width: 40px;
            height: 40px;
            border-radius: 20px;
        }
    </style>
</head>

<body>
    <div class="login_header"><a id="link" href="javascript:;">点击,弹出登录框</a></div>
    <div class="login" id="login">
        <div id="title" class="login_title">登录会员
            <span><a href="javascript:;" class="close-login">关闭</a></span>
        </div>
        <div class="login_input_content">
            <div class="login_input">
                <label>用户名:</label>
                <input type="text" name="uname">
            </div>
            <div class="login_input">
                <label>密码:</label>
                <input type="password" name="pwd">
            </div>
        </div>
        <div class="login_button"><a href="javascript:;">登录</a></div>
    </div>
    <div class="login_bg"></div>
    <script>
        var login = document.querySelector('.login')
        var mask = document.querySelector('.login_bg')
        var link = document.querySelector('#link')
        var closebtn = document.querySelector('.close-login')
        var title = document.querySelector('#title')
        link.addEventListener('click', function () {
            mask.style.display = 'block'
            login.style.display = 'block'
        })
        closebtn.addEventListener('click', function () {
            mask.style.display = 'none'
            login.style.display = 'none'
        })
        //开始拖拽
        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>

JQ

<!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;
            /* box-sizing: border-box; */
        }

        .login_header {
            text-align: center;
        }

        a {
            text-decoration: none;
            color: #000;
        }

        .login {
            display: none;
            width: 512px;
            height: 280px;
            position: fixed;
            top: 50%;
            left: 50%;
            background-color: #fff;
            transform: translate(-50%, -50%);
            border: #ebebeb solid 1px;
            box-shadow: 0 0 20px #ddd;
            z-index: 999;
            text-align: center;
        }

        .login_title {
            position: relative;
            width: 100%;
            height: 40px;
            line-height: 40px;
            margin-top: 10px;
            font-weight: 800;
            cursor: move;
        }

        .login_title span {
            position: absolute;
            width: 40px;
            height: 40px;
            top: -30px;
            right: -20px;
            background-color: #fff;
            font-size: 12px;
            border: #ebebeb solid 1px;
            border-radius: 50%;
        }

        .login_input_content {
            margin-top: 20px;
        }

        .login_input {
            margin: 0 0 10px 0;
            overflow: hidden;
        }

        .login_input label {
            float: left;
            text-align: center;
            width: 90px;
            height: 35px;
            line-height: 35px;
            padding: 10px;
            font-size: 14px;
            text-align: right;
        }

        .login_input input {
            float: left;
            width: 350px;
            height: 35px;
            line-height: 35px;
            border: 1px solid #ebebeb;
            text-indent: 5px;
            margin-top: 8px;
            outline: none;
            font-weight: normal;
            font-size: 14px;
        }

        .login_button {
            width: 50%;
            margin: 30px auto 0;
            line-height: 40px;
            font-size: 14px;
            border: 1px solid #ebebeb;
            text-align: center;
        }

        .login_button a {
            display: block;
        }

        .login_bg {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, .3);
        }
    </style>
    <script src="./lib/jquery-3.6.0.min.js"></script>
</head>

<body>
    <div class="login_header"><a id="link" href="javascript:;">点击,弹出登录框</a></div>
    <div class="login">
        <div class="login_title">
            会员登录
            <span><a href="javascript:;" class="close_btn">关闭</a></span>
        </div>
        <div class="login_input_content">
            <div class="login_input">
                <label for="uname">用户名:</label>
                <input type="text" name="username" id="uname" />
            </div>
            <div class="login_input">
                <label for="pwd">密码:</label>
                <input type="password" name="password" id="pwd" />
            </div>
        </div>
        <div class="login_button"><a href="javascript:;">登录</a></div>
    </div>
    <div class="login_bg"></div>
    <script>
        $('#link').click(function () {
            $('.login').show()
            $('.login_bg').show()
        })
        $('.close_btn').click(function () {
            $('.login').hide()
            $('.login_bg').hide()
        })
        $('.login_title').mousedown(function (e) {
            document.body.onselectstart = function () {
                return false;
            }
            if (e.target == e.currentTarget) {
                let x = e.pageX - $('.login').offset().left
                let y = e.pageY - $('.login').offset().top
                $(document).mousemove(move);

                function move(e) {
                    window.getSelection().empty() //进制选中文字
                    //之前用$('.login').css() 设置left和top 始终有偏差 可能和布局有关 所以直接用offset方法设置了
                    $('.login').offset({
                        left: e.pageX - x,
                        top: e.pageY - y
                    })
                }
                $(document).mouseup(function () {
                    $(document).off('mousemove')
                })
            }
        })
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值