可拖拽内容

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>可拖拽内容</title>
<style>
*{
width:500px;
height:300px;

}
 #div1{
width:200px;
height:200px;
border:1px solid red;
background:#999;
position:absolute;
}


#div_title{
width:100%;
height:30px;
background:#333;
}
#div_title:hover{
cursor:move;
}
#div_content{

}
</style>
<script>
    window.οnlοad=function(){
var winWidth=document.documentElement.clientWidth; //获取屏幕的宽度:文档的文档元素的客户端宽度
var winHeight=document.documentElement.clientHeight;

   var obj= document.getElementById("div1");// 获取div对象
var width=0,height=0;
//console.info(obj);

//取样式表中的样式属性  就是获取当前对象的属性 
if( typeof window.getComputedStyle!='undefined'){ //w3c中的标准
            width=window.getComputedStyle(obj,null)["width"];
height=window.getComputedStyle(obj,null)["height"];
console.info(width+" "+height);  

}else if(typeof this.elements[obj].currentStyle!='undefined'){//IE中的标准
width=this.elements[obj].currentStyle["width"];
height=this.elements[obj].currentStyle["height"];
}
obj.style.left=(parseInt(winWidth)-parseInt(width))/2+"px";  
obj.style.top=(parseInt(winHeight)-parseInt(height))/2+"px";

var div=document.getElementById("div_title");
 //console.info(div);
var isDown=false,currentX=0;currentY=0,nowX=0,nowY=0;

div.onmousedown = function(e){  //记录当前鼠标的位置 ,然后标记此时用户已经按下鼠标   
 nowX = parseInt(obj.style.left);
 nowY =parseInt(obj.style.top);
 isDown = true;
 currentX = e.clientX;
 currentY = e.clientY;
}

div.onmouseup =function(){//将鼠标 按下的标志量为false
  isDown = false;
}

div.onmousemove = function(e){
//算偏移量
  if( isDown){
  obj.style.left =  (e.clientX-currentX+nowX)+"px";
  obj.style.top = (e.clientY-currentY+nowY)+"px";
           }
}  
}
 
</script>
</head>


<body>
<div id="div1"> 
  <div id="div_title">
  
  </div>
  <div id="div_content">
  
  </div>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值