朗逸和寶來哪個檔次高 10萬左右口碑最好的車
2024-07-02
更新時間:2024-07-02 00:04:30作者:未知
1.點擊彈出框,狀態(tài)框和遮擋層(背景變灰)就會顯示出來 display:block;
2.點擊關閉按鈕,模態(tài)框和遮擋層就會隱藏起來 display:none;
3.在頁面中拖拽的原理:鼠標按下并且移動,之后松開鼠標
4.觸發(fā)事件是鼠標按下mousedown,鼠標移動mousemove,鼠標松開mouseup
5.推拽過程:鼠標移動過程中,獲得最新的值賦給模態(tài)框的left和top值,這樣模態(tài)框就可以跟著鼠標走了
6.鼠標按下出發(fā)的事件源是最上面一行,就是id為title
7.鼠標的坐標減去鼠標在盒子內(nèi)的坐標,才是真正模態(tài)框的位置
8.鼠標按下,我們要得到鼠標在盒子的坐標
9.鼠標移動,就讓模態(tài)框的坐標設置為:鼠標坐標減去盒子坐標即可,注意移動事件寫到按下事件里面
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>