鼠标点击div移动时,div自己向下移动距离
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100%;
height: 100%;
}
h1 {
font-weight: 400;
text-align: center;
cursor: pointer;
}
.zeDan {
display: none;
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, .3);
}
.login {
display: none;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, 50%);
width: 540px;
height: 330px;
background-color: #fff;
}
.login .title {
height: 70px;
padding-top: 40px;
text-align: center;
font-weight: 400;
font-size: 22px;
}
.login form {
margin-top: 22px;
padding-left: 30px;
width: 490px;
text-align: right;
}
.login form .input01 {
margin-top: 20px;
margin-left: 5px;
width: 320px;
height: 40px;
outline: none;
border: none;
}
.login form #loginTab {
width: 200px;
height: 40px;
margin-top: 30px;
margin-right: 120px;
}
.login span {
position: absolute;
top: -25px;
right: -25px;
width: 50px;
height: 50px;
border-radius: 50%;
text-align: center;
line-height: 50px;
background-color: #eee;
cursor: pointer;
}
</style>
<script>
window.addEventListener('load', function () {
let h1 = document.querySelector('.h1');
let zeDan = document.querySelector('.zeDan');
let login = document.querySelector('.login');
let span = document.querySelector('span');
let title = document.querySelector('.title');
h1.addEventListener('click', function () {
zeDan.style.display = 'block';
login.style.display = 'block';
});
span.addEventListener('click', function () {
zeDan.style.display = 'none';
login.style.display = 'none';
});
title.addEventListener('mousedown', function (e) {
let x = e.pageX - login.offsetLeft;
let 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>
</head>
<body>
<h1 class="h1">点击,弹出登录框</h1>
<div class="zeDan"></div>
<div class="login">
<div class="title">登录会员</div>
<form action="javascript:;">
用户名: <input type="text" class="input01"><br>
登录密码: <input type="password" class="input01">
<input type="submit" value="登录会员" id="loginTab">
</form>
<span>关闭</span>
</div>
</body>
</html>
点击title时会向下弹一段距离,如何解决,谢谢~