拖拽框
效果

點擊提示文本,會彈出登錄框和遮罩層;滑鼠按住左鍵移動時表單框也會跟隨滑鼠移動,滑鼠松開則靜止;點擊關閉按鈕遮罩層與登錄框消失,

知識點
元素偏移量offset常用屬性
①element.offsetParent:回傳作為該元素帶有定位的父級元素,如果父級都沒有定位則回傳body
②element.offsetTop:回傳元素相對帶有定位父元素上方的偏移
③element.offsetLeft:回傳元素相對帶有定位父元素左方的偏移
④element.offsetWidth:回傳自身包括padding、邊框、內容區的寬度,回傳值不帶單位
⑤element.offsetHeight:回傳自身包括padding、邊框、內容區的高度,回傳值不帶單位
滑鼠事件物件
pageX、pageY:相對于檔案邊緣的坐標
核心思路
- 設定好上方文本、遮罩層、登錄框的css樣式,隱藏遮罩層和登錄框display:none;
- 設定登錄框的滑鼠點擊事件,被點擊時登錄框跟隨,松開可以停止拖動模態框移動
- 滑鼠的坐標減去滑鼠在盒子內的坐標,就是模態框真正的坐標,賦值給top和left
代碼
<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="username">
</div>
<div class="login-input">
<label>登錄密碼:</label>
<input type="password" placeholder="請輸入登錄密碼" name="info[password]" id="password" class="password">
</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>
* {
padding: 0;
margin: 0;
border: 0;
}
a {
color: black;
text-decoration: none;
}
/* 標題 */
.login-header {
width: 100%;
height: 100%;
text-align: center;
margin-top: 10px;
font-size: 30px;
}
/* 彈窗 */
.login {
display: none;
width: 512px;
height: 280px;
position: fixed;
border: #ebebeb solid 1px;
left: 50%;
top: 50%;
transform: translate3d(-50%, -50%, 0);
background: #fff;
box-shadow: 0px 0 20px #ddd;
z-index: 9999;
}
/* 頂部“登錄會員” */
.login-title {
position: relative;
width: 100%;
height: 40px;
margin: 10px 0 0 0;
text-align: center;
line-height: 40px;
font-size: 18px;
cursor: move;
-webkit-user-drag: none;
}
/* 關閉按鈕 */
.login-title span {
position: absolute;
top: -30px;
right: -20px;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #fff;
font-size: 12px;
border: #ebebeb solid 1px;
}
.login-input-content {
margin-top: 20px;
}
.login-input {
overflow: hidden;
margin: 0 0 20px 0;
}
/* 表單左側的內容 */
.login-input label {
float: left;
width: 90px;
height: 35px;
text-align: right;
line-height: 35px;
padding-right: 10px;
font-size: 14px;
}
.login-input input {
float: left;
line-height: 35px;
height: 35px;
width: 350px;
border: #ebebeb 1px solid;
text-indent: 5px;
outline: none;
}
.login-button {
width: 50%;
margin: 30px auto 0 auto;
line-height: 40px;
font-size: 14px;
border: #ebebeb 1px solid;
text-align: center;
}
.login-button a {
display: block;
}
/* 遮罩層 */
.login-bg {
display: none;
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, .3);
}
window.addEventListener('load', function() {
var header = document.querySelector('.login-header');
var login = document.querySelector('.login');
var bg = document.querySelector('.login-bg');
var close = document.querySelector('.close-login');
header.addEventListener('click', function() {
login.style.display = 'block';
bg.style.display = 'block';
})
close.addEventListener('click', function() {
login.style.display = 'none';
bg.style.display = 'none';
})
var title = document.querySelector('#title');
title.addEventListener('mousedown', function(e) {
// x和y是滑鼠在登陸框內的位置
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);
})
})
})
難錯點
- 滑鼠點擊、移動、松開事件
滑鼠移動、松開是基于點擊的前提才能實作的,因此要把移動和松開事件寫在點擊事件內部,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/290108.html
標籤:其他
