<!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>拖拽模态框</title>
<style>
body{
padding: 0;
margin: 0;
/* position: relative; */
}
.btn-login{
margin-top: 30px;
text-align: center;
z-index: 100;
}
.mask{
display: none;
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
/* background: #ccc; */
background: rgba(0, 0, 0, 0.7);
z-index: 1;
/* overflow: auto; */
}
.login{
display: none;
position: absolute;
width: 500px;
height: 200px;
border: 1px #ccc solid;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
z-index: 100;
}
.login-closeBtn{
/* display: block; */
width: 0;
height: 0;
position: absolute;
top: -12px;
right: -12px;
text-align: center;
border: 15px white solid;
border-radius: 50%;
box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.2);
}
.login-closeBtn a{
display: inline-block;
position: absolute;
top: -10px;
right: -20px;
color: black;
width: 40px;
height: 40px;
line-height: 20px;
font-size: 10px;
text-decoration: none;
text-align: center;
}
.login-header{
height: 40px;
width: 100%;
text-align: center;
line-height: 40px;
font-weight: 600;
}
.input-info{
height: 40px;
/* width: 200px; */
padding: 10px 40px;
}
input{
height: 25px;
width: 300px;
outline: none;
/* display: inline; */
}
button{
height: 35px;
width: 200px;
display: block;
margin: auto;
background-color: white;
border: 1px #ccc solid;
}
</style>
</head>
<body>
<div class="btn-login">点击,弹出登录框</div>
<div class="login">
<div class="login-closeBtn"><a href="javascript:;">关闭</a></div>
<div class="login-header">登录会员</div>
<div class="input-info">
<label >用户名:</label>
<input type="text" placeholder="请输入用户名">
</div>
<div class="input-info">
<label>登录密码:</label>
<input type="password" placeholder="请输入登录密码">
</div>
<button>登录会员</button>
</div>
<div class="mask"></div>
<script>
var login = document.querySelector('.login');
var loginHeadr = document.querySelector('.login-header')
var btnLogin = document.querySelector('.btn-login')
var mask = document.querySelector('.mask')
var closeBtn = document.querySelector('.login-closeBtn')
btnLogin.onclick = function(){
mask.style.display = 'block';
login.style.display = 'block';
}
closeBtn.onclick = function(){
mask.style.display = 'none';
login.style.display = 'none';
}
//拖拽框实现
//1.header事件触发:mousedown->mousemove->mouseup
//2.mousedown:获取鼠标在模态框中坐标=鼠标在页面坐标-模块框在页面坐标
//3.mousemove:模态框在页面坐标动态变化= 鼠标在页面坐标 - 鼠标在模态框中的坐标
//4.mouseup:移除mousemove事件
loginHeadr.addEventListener('mousedown', function(e){
var x = e.pageX - login.offsetLeft
var y = e.pageY - login.offsetTop;
console.log(x, y);
loginHeadr.addEventListener('mousemove', move)
function move(e){
login.style.left = e.pageX - x + 'px';
login.style.top = e.pageY - y + 'px';
}
loginHeadr.addEventListener('mouseup', function(){
loginHeadr.removeEventListener('mousemove', move)
})
})
</script>
</body>
</html>