模态框拖拽js实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>模态框拖拽</title>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #000;
}
.login-bg {
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background-color: rgba(0, 0, 0, .3);
}
.login-header {
width: 100%;
height: 30px;
line-height: 30px;
text-align: center;
font-size: 24px;
}
.login {
position: fixed;
width: 512px;
height: 280px;
border: 1px solid #ebebeb;
background-color: #fff;
box-shadow: 0px 0px 20px #ddd;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 999;
}
.login-title {
width: 100%;
margin-top: 10px;
cursor: move;
height: 40px;
line-height: 40px;
text-align: center;
font-size: 18px;
position: relative;
}
.login-input-content {
margin-top: 20px;
}
.login-input {
overflow: hidden;
margin-bottom: 20px;
}
.login-input input.list-input {
float: left;
border: 1px solid #eee;
width: 350px;
height: 35px;
line-height: 35px;
text-indent: 5px;
}
.login-input label {
float: left;
width: 90px;
height: 35px;
text-align: right;
line-height: 35px;
font-size: 14px;
padding-right: 10px;
}
.login-title span {
position: absolute;
right: -20px;
top: -30px;
width: 40px;
height: 40px;
font-size: 12px;
border-radius: 20px;
text-align: center;
line-height: 40px;
background-color: #fff;
border: 1px solid #ebebeb;
}
.login-button {
width: 50%;
border: 1px solid #ebebeb;
text-align: center;
line-height: 40px;
font-size: 14px;
margin: 30px auto 0 auto;
}
.login-button a {
display: block;
}
</style>
<script>
window.onload = function() {
//1.获取元素
var link = document.getElementById('link');
var bg = document.getElementById('bg');
var login = document.getElementById('login');
var closeBtn = document.getElementById('closeBtn');
var loginTile = document.getElementById('title');
//2.点击弹出层这个链接,让遮盖层和登录框显示出来
link.addEventListener('click', function() {
bg.style.display = "block";
login.style.display = "block";
});
//3.点击关闭按钮,让遮盖层和登录框隐藏
closeBtn.addEventListener('click', function() {
bg.style.display = "none";
login.style.display = "none";
});
//4.开始拖拽
//(1)鼠标按下,获得鼠标在盒子内的坐标
loginTile.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>
</head>
<body>
<div class="login-header">
<a id="link" href="javascript:;">点击,弹出登录框</a>
</div>
<div class="login" id="login" style="display: none;">
<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="请输入用户名" id="username" class="list-input" />
</div>
<div class="login-input">
<label>登录密码:</label>
<input type="password" placeholder="请输入登录密码" id="password" class="list-input" />
</div>
</div>
<div class="login-button" id="loginBtn">
<a href="javascript:void(0);" id="login-button-submit">登录会员</a>
</div>
</div>
<div id="bg" class="login-bg" style="display: none;"></div>
</body>
</html>