JavaScript實現(xiàn)可拖動模態(tài)框
本文實例為大家分享了JavaScript實現(xiàn)可拖動模態(tài)框的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
HTML代碼部分:
<style>
* {
margin: 0px;
padding: 0px;
}
.login-header {
width: 100%;
text-align: center;
height: 30px;
font-size: 24px;
line-height: 30px;
cursor: pointer;
}
.login {
display: none;
width: 500px;
height: 280px;
position: fixed;
border: 1px #ebebeb solid;
left: 50%;
top: 50%;
background: #fff;
box-shadow: 0px 0px 20px #ddd;
z-index: 999;
transform: translate(-50%,-50%);
}
.login-title {
width: 100%;
margin: 10px 0px 0px 0px;
text-align: center;
height: 40px;
line-height: 40px;
font-size: 10px;
position: relative;
cursor: move;
}
.login-title span {
position: absolute;
font-size: 12px;
right: -20px;
top: -30px;
background-color: #fff;
border: 1px #ebebeb solid;
width: 40px;
height: 40px;
border-radius: 20px;
}
.login-input-content {
margin-top: 20px;
}
.login-button {
width: 100px;
margin: 30px auto 0px auto;
line-height: 40px;
font-size: 14px;
border: 1px #ebebeb solid;
text-align: center;
}
a {
text-decoration: none;
color: #000;
}
.login-button a {
display: block;
}
.login-input input.list-input {
float: left;
line-height: 35px;
height: 35px;
width: 350px;
border: 1px #ebebeb solid;
text-indent: 5px;
}
.login-input {
overflow: hidden;
margin: 0px 0px 20px 0px;
}
.login-input label {
float: left;
width: 90px;
padding-right: 10px;
height: 35px;
line-height: 35px;
text-align: right;
font-size: 14px;
}
.login-mask {
display: none;
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background-color: rgba(0, 0, 0, .3);
}
</style>
</head>
<body>
<div class="login-header">點擊,彈出登錄框</div>
<div id="login" class="login">
<div id="title" class="login-title">登錄會員<span><a id="closeBtn" class="close-login" href="javascript:void(0);" >關閉</a></span></div>
<div class="login-input-content">
<div class="login-input">
<label>用戶名:</label>
<input type="text" placeholder="請輸入用戶名" id="username" class="list-input">
</div>
<div class="login-input">
<label>登錄密碼:</label>
<input type="password" placeholder="請輸入登錄密碼" id="password" class="list-input">
</div>
</div>
<div id="loginBtn" class="login-button"><a id="login-button-submit" href="javascript:void(0);" >登錄會員</a></div>
</div>
<!-- 遮罩層 -->
<div id="mask" class="login-mask"></div>
JS部分:
<script>
// 1.獲取元素
var login = document.querySelector('.login');
var mask = document.querySelector('.login-mask');
var loginHeader = document.querySelector('.login-header');
var closeBtn = document.querySelector('.close-login');
var loginTitle = document.querySelector('.login-title');
// 2.點擊登錄提示 讓login和mask顯示出來;
loginHeader.addEventListener('click', function() {
login.style.display = 'block';
mask.style.display = 'block';
})
// 3.點擊關閉按鈕 隱藏login和mask;
closeBtn.addEventListener('click', function() {
login.style.display = 'none';
mask.style.display = 'none';
})
// 4.拖拽登錄框
// 4.1 鼠標按下獲得鼠標在盒子中的坐標
loginTitle.addEventListener('mousedown', function(e) {
var x = e.pageX-login.offsetLeft;
var y = e.pageY-login.offsetTop;
// 4.2 鼠標移動的時候,把鼠標在頁面中的坐標 減去 鼠標在盒子中的坐標,得到login盒子的left和top值
document.addEventListener('mousemove', move)
function move(event) {
login.style.left = event.pageX - x + 'px';
login.style.top = event.pageY - y + 'px';
}
// 4.3 鼠標松開的時候,移除移動事件
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', move)
})
})
</script>
效果演示:

思路:
給可拖動部分添加點擊事件,觸發(fā)時計算鼠標在可拖動部分中的坐標( e.pageX - box.offsetLeft ),得到 x y ,然后給 document 添加鼠標移動事件,因為當鼠標拖動模態(tài)框的時候,是在整個DOM窗口內(nèi)移動的。保持鼠標與模態(tài)框的相對位置不變,所以需要計算此時的模態(tài)框的位置(e.pageX - x ),進而修改模態(tài)框位置。當鼠標彈起時,清除移動事件即可。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ES6知識點整理之函數(shù)數(shù)組參數(shù)的默認值及其解構應用示例
這篇文章主要介紹了ES6知識點整理之函數(shù)數(shù)組參數(shù)的默認值及其解構應用,結合實例形式分析了ES6函數(shù)數(shù)組參數(shù)解構賦值和默認值的設置相關操作技巧,需要的朋友可以參考下2019-04-04
js數(shù)組常用操作方法小結(增加,刪除,合并,分割等)
這篇文章主要介紹了js數(shù)組常用操作方法,結合實例總結了javascript數(shù)組的增加、刪除、合并、分割等操作技巧,需要的朋友可以參考下2016-08-08
javascript創(chuàng)建createXmlHttpRequest對象示例代碼
這篇文章主要介紹了javascript創(chuàng)建createXmlHttpRequest對象的示例代碼。需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02

