<!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>Document</title><style>
.login {
display: none;
width: 513px;
height: 280px;
position: fixed;
border: #ebebeb solid 1px;
left: 50%;
top: 50%;
background-color: #fff;
box-shadow: 0 0 20px #ddd;
z-index: 9999;
transform: translate(-50%, -50%);
}
.login-bg {
display: none;
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, .3);
}
.title {
width: 100%;
height: 50px;
line-height: 50px;
background-color: #ddd;
font-size: 25px;
text-align: center;
}
</style>
</head>
<body>
<body>
<div class="login-header">
<a href="javascript:;" id="link">点击弹出登录框</a>
</div>
<div class="login" id="login">
<div class="title" id="title">会员登录
<span><a id="closeBtn" href="javascript:void(0)" class="close-btn">关闭</a></span>
</div>
<div class="uname">
<label>用户名:</label>
<input type="text" placeholder="请输入用户名">
</div>
<div class="pwd">
<label>密码:</label>
<input type="password" placeholder="请输入密码">
</div>
</div>
<!-- 遮罩层 -->
<div id="bg" class="login-bg"></div>
</body>
<script>
var link =document.querySelector('#link')
var closebtn=document.querySelector('.close-btn')
var login =document.querySelector('.login')
var bg=document.querySelector('#bg')
var title=document.querySelector('.title')
link.addEventListener('click',function(){
login.style.display='block'
bg.style.display='block'
})
closebtn.addEventListener('click',function(){
login.style.display='none'
bg.style.display='none'
})
// 点击区域
title.addEventListener('mousedown',function(e){
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)
})
})
</script>
</body>
</html>