实现页面窗口的拖拽效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#title{
border: 3px solid gray;
background: rgba(142,229,238,0.6);
cursor: move;
}
#register{
margin-top: 200px;
margin-left:500px;
position: relative;
width: 300px;
height: 300px;
background: #00FF7F;
}
#close{
position: absolute;
right: 0;
cursor: pointer;
}
</style>
</head>
<body>
<div id="register">
<div id="tuo">
注册信息(可以拖拽)
<span id="close">【关闭】</span>
</div>
</div>
<script type="text/javascript">
var register = document.getElementById('register');
var close = document.getElementById('close');
var title= document.getElementById('title');
title.onmousedown = function (e) {
e = e || window.event;
var x = e.pageX - register.offsetLeft;
var y = e.pageY - register.offsetTop;
document.onmousemove = function(e){
boxX = e.pageX - x;
boxY = e.pageY - y;
register.style.left = boxX -500 + "px";
register.style.top = boxY -200 + "px";
}
}
document.onmouseup = function(){
document.onmousemove = null;
}
close.onclick = function(){
register.style.display = 'none';
}
</script>
</body>
</html>