1. 程式人生 > 程式設計 >JavaScript實現模態框拖拽效果

JavaScript實現模態框拖拽效果

在這裡做一個模態框拖曳的案例,在這裡要實現的功能有:

1.點選彈出層, 會彈出模態框, 並且顯示灰色半透明的遮擋層。

2.點選關閉按鈕,可以關閉模態框,並且同時關閉灰色半透明遮擋層。

3.滑鼠放到模態框最上面一行,可以按住滑鼠拖拽模態框在頁面中移動。

4.滑鼠鬆開,可以停止拖動模態框移動。

實現思路為:

點選彈出層, 模態框和遮擋層就會顯示出來display:block;

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

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

觸發事件是滑鼠按下 mousedown, 滑鼠移動mousemove 滑鼠鬆開 mouseup。

拖拽過程: 滑鼠移動過程中,獲得最新的值賦值給模態框的left和top值, 這樣模態框可以跟著滑鼠走了。

滑鼠按下觸發的事件源是 最上面一行,即<div id="title" class="login-title">登入會員。

滑鼠的座標 減去 滑鼠在盒子內的座標, 才是模態框真正的位置。

滑鼠按下,我們要得到滑鼠在盒子的座標。

滑鼠移動,讓模態框的座標 設定為 : 滑鼠座標 減去盒子座標即可,注意移動事件寫到按下事件裡面。

滑鼠鬆開,就停止拖拽,即讓滑鼠移動事件解除 。

實現程式碼為:

<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .login-header {
            width: 100%;
            text-align: center;
            height: 30px;
            font-size: 24px;
            line-height: 30px;
        }
        
        ul,li,ol,dl,dt,dd,div,p,span,h1,h2,h3,h4,h5,h6,a {
            padding: 0px;
            margin: 0px;
        }
        
        .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;
   www.cppcns.com
top: 0px; left: 0px; background: rgba(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=":;" rel="external nofollow" >點選,彈出登入框</a></div> <div id="login" class="login"> <div id="title" class="login-title">登入會員 <span><a id="closeBtn" href="script:void(0);" rel="external nofollow" rel="external nofollow" class="close-login">關閉</a></span> </div> <div class="login-input-content"> <div class="login-input"> http://www.cppcns.com <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);" rel="external nofollow" rel="external nofollow" id="login-button-submit">登入會員</a></div> </div> <!-- 遮蓋層 --> <div id="bg" class="login-bg"></div> <script> // 1. 獲取元素 var login = document.querySelector('.login'); BGOpwgXdKY var mask = document.querySelector('.login-bg'); var link = document.querySelector('#link'); var closeBtn = document.querySelector('#closeBtn'); var title = document.querySelector('#title'); // 2. 點選彈出層這個連結 link 讓mask 和login 顯示出來 link.addEventListener('click',function() { mask.style.display = 'block'; login.style.display = 'block'; }) // 3. 點選 closeBtn 就隱藏 mask 和 login closeBtn.addEventListener('click',function() { mask.style.display = 'none'; login.style.display = 'none'; }) // 4. 開始拖拽 // (1) 滑鼠按下, 獲得滑鼠在盒子內的座標 title.addEventListener('mousedown',function(e) { var x = e.pageX - login.offsetLeft; var y = e.pageY - login.offsetTop; // (2) 滑鼠移動的時候,把滑鼠在頁面中的座標,減去 滑鼠在盒子內的座標就是模態框的left和top值 document.addEventListener('mousemove',move) function move(e) { login.style.left = e.pageX - x + 'px'; login.style.top = e.pageY - y + 'px'; } // (3) 滑鼠彈起,就讓滑鼠移動事件移除 document.addEventListener('mouseup',function() { document.removeEventListener('mousemove',move); }) }) </script> </body> </html>

實現效果為:

JavaScript實現模態框拖拽效果

到此這篇關於JavaScript實現模態框拖拽效果的文章就介紹到這了,更多相關JavaScript模態框拖拽內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!