<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
}
.zz {
height: 100%;
width: 100%;
display: none;
position: absolute;
left: 0;
top: 0;
background-color: rgba(0, 0, 0, 0.6);
}
.box {
height: 290px;
width: 700px;
position: absolute;
left: 50%;
display: none;
top: 50%;
margin-left: -350px;
margin-top: -145px;
box-sizing: border-box;
background-color: #fff;
}
.box .move {
height: 22px;
width: 100%;
position: absolute;
top: 0;
left: 0;
cursor: move;
}
.box h2 {
height: 56px;
line-height: 56px;
text-align: center;
font-weight: normal;
font-size: 28px;
}
table {
height: 88px;
width: 480px;
margin: 10px auto;
border-collapse: collapse;
}
tr,
td {
font-size: 20px;
}
table tr td:nth-child(1) {
text-align: right;
}
input {
height: 32px;
width: 290px;
padding-left: 5px;
border: 1px solid #ccc;
}
p {
margin-top: 30px;
text-align: center;
}
button {
width: 200px;
height: 38px;
background-color: skyblue;
border: none;
cursor: pointer;
}
.close {
height: 34px;
width: 34px;
line-height: 34px;
text-align: center;
font-size: 28px;
border-radius: 50%;
position: absolute;
right: -16px;
top: -16px;
cursor: pointer;
border: 1px solid #aaa;
background-color: #fff;
}
</style>
</head>
<body>
<p class="begin">点我登陆咯!!</p>
<div class="zz"></div>
<div class="box">
<div class="move"></div>
<h2>登录会员</h2>
<table>
<tr>
<td>用户名:</td>
<td><input type="text" placeholder="请输入手机号"></td>
</tr>
<tr>
<td>登录密码:</td>
<td><input type="password" placeholder="请输入密码"></td>
</tr>
</table>
<p><button>登录</button></p>
<div class="close">×</div>
</div>
</body>
<script>
var zz = document.querySelector('.zz');
var box = document.querySelector('.box');
var begin = document.querySelector('.begin');
var close = document.querySelector('.close');
var move = document.querySelector('.move');
begin.onclick = function() {
zz.style.display = "block";
box.style.display = "block";
}
close.addEventListener('click', function() {
zz.style.display = "none";
box.style.display = "none";
});
move.addEventListener('mousedown', function(e) {
var x = e.pageX - box.offsetLeft;
var y = e.pageY - box.offsetTop;
document.addEventListener('mousemove', move);
function move(e) {
box.style.left = e.pageX - x + "px";
box.style.top = e.pageY - y + "px";
}
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', move);
});
});
</script>
</html>
老师,我不知道为什么,拖动元素一直计算错误。您帮我瞅瞅。谢谢