六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 40|回复: 0

js拖动

[复制链接]

升级  20%

2

主题

2

主题

2

主题

童生

Rank: 1

积分
10
 楼主| 发表于 2013-1-29 08:41:47 | 显示全部楼层 |阅读模式
 
<!DOCTYPE HTML><html lang="en-US"><head><meta charset="UTF-8"><title></title></head><body><script type="text/javascript">var drag={//移动的元素obj:null,//o 触发事件的元素 oroot移动的元素 x最小的x坐标 y最小的y坐标 fx修改x的坐标的函数 fy修改y坐标的函数init:function(o,oRoot){o.onmousedown=drag.start;o.root=oRoot!=null?oRoot:o;},start:function(e){//获得监控对象e = drag.fixE(e);var o= drag.obj = this;o.lastx=e.clientX;o.lasty=e.clientY;//设置对象的位置document.onmousemove = drag.drag;document.onmouseup = drag.end;return false;},drag:function(e){//移动对象e = drag.fixE(e);var x=parseInt(drag.obj.root.style.left)+e.clientX-drag.obj.lastx;var y=parseInt(drag.obj.root.style.top)+e.clientY-drag.obj.lasty;drag.obj.root.style.left=x+"px";drag.obj.root.style.top=y+"px";drag.obj.lastx=e.clientX;drag.obj.lasty=e.clientY;return false;},end:function(){//mouseupdown move监控都设置为null//监控对象也设置为空document.onmousemove=null;document.onmousedown=null;drag.obj=null;},//ie兼容fixE : function(e){if (typeof e == 'undefined') e = window.event;if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;return e;},}</script><div id="pp" style="padding:0;margin:0;position:absolute;top:10px;left:10px;width:100px;height:100px;background:rgb(200,200,100)"></div><script type="text/javascript">var ooo=document.getElementById('pp');drag.init(ooo,null);</script></body></html> 剽窃《精通javascript》里面的
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表