1. 程式人生 > 其它 >拖動模態框

拖動模態框

技術標籤:javascriptcsshtml

拖動模態框

在許多網頁中,彈出框和拖曳的使用是非常多的,今天我們就通過一個案列來學習這兩個功能。css和HTML的程式碼事先已經準備好了,接下來通過新增js實現功能
在這裡插入圖片描述
html程式碼如下:

 <div class="login-header"><a id="link" href="javascript:void(0);">點選,彈出登入框</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>

css程式碼如下:

 <style>
         * {
            padding: 0px;
            margin: 0px;
        }
        .login-header {
            width: 100%;
            text-align: center;
            height: 30px;
            font-size: 24px;
            line-height: 30px;
        }
        .login {
            width: 512px;
            height: 280px;
            position: absolute;
            border: #ebebeb solid 1px;
            left: 50%;
            right: 50%;
            background: #ffffff;
            box-shadow: 0px 0px 20px #ddd;
            z-index: 9999;
            margin-left: -256px;
            margin-top: 140px;
            /* 先隱藏 */
            display: none;
        }

        .login-title {
            width: 100%;
            margin: 10px 0px 0px 0px;
            text-align: center;
            line-height: 40px;
            height: 40px;
            font-size: 18px;
            position: relative;
            cursor: move;
            -moz-user-select: none;
            /*火狐*/
            -webkit-user-select: none;
            /*webkit瀏覽器*/
            -ms-user-select: none;
            /*IE10*/
            -khtml-user-select: none;
            /*早期瀏覽器*/
            user-select: none;
        }

        .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 {
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0px;
            left: 0px;
            background: #000000;
            filter: alpha(opacity=30);
            -moz-opacity: 0.3;
            -khtml-opacity: 0.3;
            opacity: 0.3;
            display: none;
        }

        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>

案例分析:
第一步:
1.點選彈出框,模態框和遮擋層顯示出來display:block;
2.點選關閉按鈕後,又被隱藏起來:display:none;

// 1.獲取元素(login,bg,按鈕和關閉按鈕)
    var login=document.getElementById('login')
    var bg=document.getElementById('bg')
    var link=document.getElementById('link')
    var closeBtn=document.getElementById('closeBtn')
    var title=document.getElementById('title')
    // 點選按鈕,彈出框和遮蓋層
    link.onclick=function(){
        login.style.display='block';
        bg.style.display='block'
    }
    //點選關閉按鈕,隱藏彈出框
    closeBtn.addEventListener('click',function(){
        login.style.display='none';
        bg.style.display='none'
    })

第二步:拖曳
1.滑鼠放到模態框最上面一行,可以按住滑鼠拖曳模態框在頁面中移動
2.滑鼠鬆開後,停止拖曳
觸發事件是滑鼠按下:
mousedown,滑鼠移動:mousemove,滑鼠鬆開:mouseup.

在這裡插入程式碼片
```//拖曳
            //(1) 當滑鼠按下,獲得滑鼠在盒子中的座標
            // 滑鼠在盒子中的位置=滑鼠頁面的位置-盒子的座標
        title.addEventListener('mousedown', function(e) {
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop
                //(2)滑鼠移動後,盒子的座標
                //盒子的座標=滑鼠頁面位置-滑鼠在盒子中的位置
            document.addEventListener('mousemove', move)

            function move(e) {
                var loginX = e.pageX - x;
                var loginY = e.pageY - y;
                login.style.left = loginX + 250 + 'px';
                login.style.top = loginY - 140 + 'px';
            }
            //(3)移除
            document.addEventListener('mouseup', function() {
                document.removeEventListener('mousemove', move)
            })
        })