可拖拽的窗口(div)

来源:互联网 发布:天下3英雄榜数据 编辑:程序博客网 时间:2024/06/05 05:28



<!DOCTYPE html>

<html id="hId">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style type="text/css">


*{
margin:0px;
padding:0px;
}


#box{
position:absolute;
left:300px;
top:100px;
width:300px;
height:200px;
border:1px solid black;
}


#title{
width:300px;
height:30px;
background-color:green;
text-align:center;
}




#content{
width:300px;
height:170px;
}




</style>


<body>
<div id="box">
<div id="title">
标题
</div>
<div id="content">
内容内容内容内容
</div>
</div>
</body>
</html>


<script type="text/javascript">


window.onload = function(){
var isDown = false;
var offsetX =0;
var offsetY =0;
document.getElementById("title").onmousedown = function(event){
var evt = event || window.event;
offsetX = evt.offsetX;
offsetY = evt.offsetY;
isDown=true;
//this.style.cursor = "pointer";
this.style.cursor = "pointer";
}

document.onmouseup = function(){
isDown=false;
}

document.onmousemove = function(event){
if(isDown){
var box = document.getElementById("box");
var evt = event || window.event;
box.style.left = (evt.pageX-offsetX)+"px";
box.style.top = (evt.pageY-offsetY)+"px";
}
}
}

</script>
原创粉丝点击