滑鼠移動 登陸框跟隨 註冊登陸 註冊信息 (可以拖拽) 【關閉】 ...
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>滑鼠移動 登陸框跟隨</title>
</head>
<style type="text/css">
head,body{
margin:0;
padding:0;
}
.top{
height:30px;
background:#336699;
padding-left:20px;
color:white;
line-height:30px;
}
.regBox{
width:310px;
position:absolute;
top:228px;
left:528px;
}
.register{
cursor:pointer;
}
.regMessage{
width:300px;
height:200px;
display:none;
border:5px solid #c0c0c0;
}
.regMessage .dt{
background:#336666;
height:30px;
line-height:30px;
color:#fbfbfb;
}
.regMessage .dt span{
cursor:move;
}
.regMessage .dt span:nth-child(1){
float:left;
}
.regMessage .dt span:nth-child(2){
float:right;
}
</style>
<body>
<div class="top">
<span class="register">註冊登陸</span>
</div>
<div class="regBox">
<div class="regMessage">
<div class="dt"><span>註冊信息 (可以拖拽) </span><span class="close">【關閉】</span></div>
</div>
</div>
<script type="text/javascript">
function show(ele){
ele.style.display="block";
}
function hide(ele){
ele.style.display="none";
};
var register= document.querySelector(".register");
var regMessage = document.querySelector(".regMessage");
var regBox = document.querySelector(".regBox");
var close = document.querySelector(".close");
register.addEventListener("click",function(e)
{
show(regMessage);
},false);
close.addEventListener("click",function(e)
{
e.stopPropagation();
hide(regMessage);
},false);
regMessage.children[0].onmousedown=function(event){
var e=event||window.event;
var left=e.clientX-regBox.offsetLeft;
var top=e.clientY-regBox.offsetTop;
document.onmousemove=function(event){
var event=event||window.event;
var x=event.clientX-left;
var y=event.clientY-top;
//邊界檢測
if(x<=0){
x=0;
}
if(x>=1366-regBox.offsetWidth){
x=1366-regBox.offsetWidth;
}
if(y<=0){
y=0;
}
if(y>=600-regBox.offsetHeight){
y=600-regBox.offsetHeight;
}
regBox.style.top=y+"px";
regBox.style.left=x+"px";
//防止移動的過程選中字體 字體也是大盒子的一部分
//所以移動的時候 選中之後 即使抬起滑鼠也會移動
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
}
regMessage.children[0].onmouseup=function(){
document.onmousemove=null;
}
}
</script>
</body>
</html>