jsp彈出自定義對話框:js彈出選擇對話框

分析

1.點擊彈出框,狀態框和遮擋層(背景變灰)就會顯示出來 display:block;

2.點擊關閉按鈕,模態框和遮擋層就會隱藏起來 display:none;

3.在頁面中拖拽的原理:鼠標按下並且移動,之後鬆開鼠標

4.觸發事件是鼠標按下mousedown,鼠標移動mousemove,鼠標鬆開mouseup

5.推拽過程:鼠標移動過程中,獲得最新的值賦給模態框的left和top值,這樣模態框就可以跟着鼠標走了

6.鼠標按下出發的事件源是最上面一行,就是id為title

7.鼠標的坐標減去鼠標在盒子內的坐標,才是真正模態框的位置

8.鼠標按下,我們要得到鼠標在盒子的坐標

9.鼠標移動,就讓模態框的坐標設置為:鼠標坐標減去盒子坐標即可,注意移動事件寫到按下事件裏面

10.鼠標鬆開,就是停止拖拽,就是可以讓鼠標移動事件解除

代碼

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .login-header {
            width: 100%;
            text-align: center;
            height: 30px;
            font-size: 24px;
            line-height: 30px;
        }
        
        * {
            padding: 0;
            margin: 0;
        }
        
        .login {
            display: none;
            width: 512px;
            height: 280px;
            position: fixed;
            border: #ebebeb solid 1px;
            left: 50%;
            top: 50%;
            background: #ffffff;
            box-shadow: 0px 0px 20px #ddd;
            z-index: 9999;
            transform: translate(-50%, -50%);
        }
        
        .login-title {
            width: 100%;
            margin: 10px 0px 0px 0px;
            text-align: center;
            line-height: 40px;
            height: 40px;
            font-size: 18px;
            position: relative;
            cursor: move;
        }
        
        .login-input-content {
            margin-top: 20px;
        }
        
        .login-button {
            width: 50%;
            margin: 30px auto 0px auto;
            line-height: 40px;
            font-size: 14px;
            border: #ebebeb 1px solid;
            text-align: center;
        }
        
        .login-bg {
            display: none;
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0px;
            left: 0px;
            background: rgba(0, 0, 0, .3);
        }
        
        a {
            text-decoration: none;
            color: #000000;
        }
        
        .login-button a {
            display: block;
        }
        
        .login-input input.list-input {
            float: left;
            line-height: 35px;
            height: 35px;
            width: 350px;
            border: #ebebeb 1px solid;
            text-indent: 5px;
        }
        
        .login-input {
            overflow: hidden;
            margin: 0px 0px 20px 0px;
        }
        
        .login-input label {
            float: left;
            width: 90px;
            padding-right: 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: #ffffff;
            border: #ebebeb solid 1px;
            width: 40px;
            height: 40px;
            border-radius: 20px;
        }
    </style>

</head>

<body>
    <div class="login-header"><a id="link" href="javascript:;">點擊,彈出登錄框</a></div>
    <div id="login" class="login">
        <div id="title" class="login-title">登錄會員
            <span><a id="closeBtn" href="javascript:void(0);" class="close-login">關閉</a></span>
        </div>
        <div class="login-input-content">
            <div class="login-input">
                <label>用戶名:</label>
                <input type="text" placeholder="請輸入用戶名" name="info[username]" id="username" class="list-input">
            </div>
            <div class="login-input">
                <label>登錄密碼:</label>
                <input type="password" placeholder="請輸入登錄密碼" name="info[password]" id="password" class="list-input">
            </div>
        </div>
        <div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登錄會員</a></div>
    </div>
    <!-- 遮蓋層 -->
    <div id="bg" class="login-bg"></div>

    <script>
        var link = document.querySelector('#link'); //獲取我們鼠標點擊的對象
        var login = document.querySelector('.login'); //獲得彈出框元素
        var mask = document.querySelector('#bg');
        var closeBtn = document.querySelector('#closeBtn');
        link.addEventListener('click', function() {
            login.style.display = 'block';
            mask.style.display = 'block';

        })
        closeBtn.addEventListener('click', function() {
            login.style.display = 'none';
            mask.style.display = 'none';

        })
        var title = document.querySelector('#title');
        title.addEventListener('mousedown', function(e) {
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            console.log(y);

            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>

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/227974.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 21:26
下一篇 2024-12-09 21:26

相關推薦

發表回復

登錄後才能評論