javascript實現(xiàn)移動的模態(tài)框效果
本文實例為大家分享了javascript實現(xiàn)移動的模態(tài)框效果的具體代碼,供大家參考,具體內(nèi)容如下
頁面效果:
點擊鏈接后,彈出登錄模態(tài)框,點擊關(guān)閉鏈接可以關(guān)閉模態(tài)框,鼠標(biāo)在模態(tài)框標(biāo)題區(qū)域按下后可以拖拽模態(tài)框,松開鼠標(biāo)后,模態(tài)框停止移動
實現(xiàn)思路:
1、html、css搭建好頁面,設(shè)置好模態(tài)框內(nèi)容和樣式后,將模態(tài)框隱藏:display: none;如果點擊彈出模態(tài)框后,頁面背景色發(fā)生改變,可以添加一個遮罩層,將遮罩層也先隱藏
2、給點擊后彈出模態(tài)框的元素添加點擊事件- - -onclick
事件處理程序中設(shè)置- - -模態(tài)框 和 遮罩層 顯示- - -eg:
login.style.display = ‘block'; loginBg.style.display = ‘block';
3、給關(guān)閉模態(tài)框元素添加點擊事件- - -onclick
事件處理程序中設(shè)置- - -模態(tài)框 和 遮罩層 隱藏- - -eg:
login.style.display = ‘none'; loginBg.style.display = ‘none';
4、給模態(tài)框標(biāo)題部分添加鼠標(biāo)按下事件- - -mousedown
獲取鼠標(biāo)在模態(tài)框中的位置
5、鼠標(biāo)按下事件中再添加鼠標(biāo)移動事件- - -mousemove
document.addEventListener(‘mousemove', move);
獲取鼠標(biāo)在頁面中的位置
鼠標(biāo)的位置值 - 鼠標(biāo)在模態(tài)框中的位置值 = 模態(tài)框在頁面中的位置值
將計算得出的位置值賦值給模態(tài)框的top、left,就可以達到拖拽鼠標(biāo)的效果,跟隨鼠標(biāo)移動
6、當(dāng)鼠標(biāo)松開后,模態(tài)框要停止移動。鼠標(biāo)按下事件中再添加鼠標(biāo)松開事件- - -mouseup
鼠標(biāo)松開事件處理程序:頁面移除鼠標(biāo)移動事件- - -document.removeEventListener(‘mousemove’, move);
注意:要添加移除事件,所以給鼠標(biāo)移動函數(shù)單獨拿出來,寫一個函數(shù)名,添加和移除事件時引用函數(shù)名就可以了
代碼示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>拖動的模態(tài)框</title>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #000;
}
.login-header {
margin: 100px auto;
height: 30px;
line-height: 30px;
font-size: 24px;
text-align: center;
}
.login {
display: none;
width: 515px;
height: 282px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 1px solid #ebebeb;
background-color: #fff;
box-shadow: 0px 0px 20px #ddd;
text-align: center;
z-index: 99999;
}
.login-title {
position: relative;
height: 50px;
line-height: 50px;
font-size: 18px;
cursor: move;
}
.close {
position: absolute;
top: 0;
right: 0;
transform: translate(50%, -50%);
width: 36px;
height: 36px;
line-height: 36px;
font-size: 12px;
border-radius: 18px;
border: 1px solid #ddd;
color: #666;
background-color: #fff;
}
.login-input-content {
margin: 10px 0;
}
.login-input label {
display: inline-block;
width: 80px;
text-align: right;
}
.login-input input {
width: 300px;
height: 40px;
margin: 10px 0;
padding-left: 10px;
border: 1px solid #ddd;
outline-color: royalblue;
}
.loginBtn a {
display: block;
width: 180px;
height: 35px;
line-height: 35px;
margin: 10px auto;
border: 1px solid #ddd;
}
.login-bg {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .3);
}
</style>
</head>
<body>
<div class="login-header"><a id="link" href="javascript:;" >點擊,彈出登錄框</a></div>
<div class="login">
<div class="login-title">
登錄會員
<span><a class="close" href="javascript:void(0);" >關(guān)閉</a></span>
</div>
<div class="login-input-content">
<div class="login-input">
<label for="username">用戶名:</label>
<input type="text" name="info[username]" id="username" placeholder="請輸入用戶名"><br>
</div>
<div class="login-input">
<label for="password">登錄密碼:</label>
<input type="password" name="info[password]" id="password" placeholder="請輸入登錄密碼"><br>
</div>
</div>
<div type="submit" value="登錄會員" class="loginBtn"><a href="javascript:void(0);" >登錄會員</a></div>
</div>
<!-- 遮蓋層 -->
<div class="login-bg"></div>
<script>
var link = document.querySelector('#link');
var login = document.querySelector('.login');
var loginBg = document.querySelector('.login-bg');
var close = document.querySelector('.close');
var loginTitle = document.querySelector('.login-title');
link.addEventListener('click', function() {
login.style.display = 'block';
loginBg.style.display = 'block';
})
close.addEventListener('click', function() {
login.style.display = 'none';
loginBg.style.display = 'none';
})
loginTitle.addEventListener('mousedown', function(e) {
// 計算出鼠標(biāo)按下時,鼠標(biāo)在模態(tài)框中的位置
var x = e.pageX - login.offsetLeft;
var y = e.pageY - login.offsetTop;
// 給移動的模態(tài)框賦值位置
function move(e) {
login.style.left = e.pageX - x + 'px';
login.style.top = e.pageY - y + 'px';
}
// 添加鼠標(biāo)事件,按下鼠標(biāo)時模態(tài)框跟著鼠標(biāo)移動
document.addEventListener('mousemove', move);
// 鼠標(biāo)松開時,模態(tài)框停止移動
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', move);
})
})
</script>
</body>
</html>頁面效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中使用replace結(jié)合正則實現(xiàn)replaceAll的效果

