码迷,mamicode.com
首页 > 其他好文 > 详细

拖拽demo

时间:2017-08-20 13:33:00      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:clientx   utf-8   null   poi   height   cli   doc   off   pointer   

<!doctype html>
<head>
<meta charset="utf-8">
<style>
*{margin:0;padding:0;}
#odiv{height:200px;width:300px;background-color:red;position:absolute;left:400px;top:300px;}
#son{height:30px;width:300px;background-color:pink;cursor:pointer;}
</style>
</head>
<body>
<div id="odiv">
<div id="son">
</div>
</div>
<script>

function drop(bar,target){
bar.onmousedown=drag;
function drag(event){
      event=event||window.event;
      var ox=event.clientX-target.offsetLeft,oy=event.clientY-target.offsetTop;
      document.onmousemove=function(event){
           event=event||window.event;
           var oh=document.documentElement.clientHeight,ow=document.documentElement.clientWidth,
               maxy=oh-target.offsetHeight,maxx=ow-target.offsetWidth;
           var x=event.clientX-ox,y=event.clientY-oy;
               if(x<0)x=0;
                 else if(x>maxx)x=maxx;
               if(y<0)y=0;
                 else if(y>maxy)y=maxy;
           target.style.left=x+"px";
           target.style.top=y+"px";
};
      document.onmouseup=function(){
           document.onmousemove=null;
           document.onmouseup=null;
};
}}

var target=document.getElementById("odiv"),bar=document.getElementById("son");
drop(bar,target);
</script>
</body>
</html>

 

拖拽demo

标签:clientx   utf-8   null   poi   height   cli   doc   off   pointer   

原文地址:http://www.cnblogs.com/eco-just/p/7399679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!