- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title> 鼠标拖动层(可任意绑定DIV标签)(一) </title>
- <script language="javascript" type="text/javascript">
- /*
- 鼠标拖动层(可任意绑定DIV标签)(一)
- 作者:Anlige
- 联系方式:1034555083/zhanghuiguoanlige@126.com
-
- 调用方法:var myDrag=new Endrag(source,target,offSetX, offSetY);
- 参数说明:source--鼠标动作绑定对象;target--操作目标对象(要移动的对象);offSetX--横坐标偏移;offSetY--纵坐标偏移
- 说明:通过多次调用本方法绑定多个对象的拖动
- */
- funs={
- index:100,
- getFocus:function (target){
- if(target.style.zIndex!=this.index){
- this.index += 2;
- var idx = this.index;
- target.style.zIndex=idx;
- }
- },
- abs:function (element) {
- var result = { x: element.offsetLeft, y: element.offsetTop};
- element = element.offsetParent;
- while (element) {
- result.x += element.offsetLeft;
- result.y += element.offsetTop;
- element = element.offsetParent;
- }
- return result;
- }
- };
-
- function Endrag(source,target,offSetX, offSetY){
- source=typeof(source)=="object" ? source:document.getElementById(source);
- target=typeof(target)=="object" ? target:document.getElementById(target);
- var x0=0,y0=0,x1=0,y1=0,moveable=false,index=100,NS=(navigator.appName=='Netscape');
- offSetX=typeof offSetX== "undefined" ? 0:offSetX;
- offSetY=typeof offSetY== "undefined" ? 0:offSetY;
- source.onmousedown=function(e){
- e = e ? e : (window.event ? window.event : null);
- funs.getFocus(target);
- if(e.button==(NS)?0 :1) {
- if(!NS){this.setCapture()}
- x0 = e.clientX ;
- y0 = e.clientY ;
- x1 = parseInt(funs.abs(target).x);
- y1 = parseInt(funs.abs(target).y);
- moveable = true;
- }
- };
- //拖动;
- source.onmousemove=function(e){
- e = e ? e : (window.event ? window.event : null);
- if(moveable){
- target.style.left = (x1 + e.clientX - x0 - offSetX) + "px";
- target.style.top = (y1 + e.clientY - y0 - offSetY) + "px";
- }
- };
- //停止拖动;
- source.onmouseup=function (e){
- if(moveable) {
- if(!NS){this.releaseCapture();}
- moveable = false;
- }
- };
- }
- </script>
- </head>
- <body onload="var i=new Endrag('hand','MoveDiv',0,0);var j=new Endrag('Div1','Div1',0,0);">
- <div id="MoveDiv" style="position:absolute;left:200px;top:50px;width:400px;height:200px;border:1px #dddddd solid;">
- <div id="hand" style="line-height:25px;text-align:center;background:#f00">
- 这个用子标签操作父标签
- </div>
- 调用方法<br />var i=new Endrag('hand','MoveDiv',0,0);<br />
- 第一个参数为源标签对象或对象id属性;第二个参数为要移动的目标对象或对象id属性;第3、4个参数为偏移值,供最后调整用;<br />
- 鼠标指针在红色区域才有效
- </div>
- <div id="Div1" style="background:#ffffff;position:absolute;left:400px;top:300px;width:300px;height:200px;border:1px #dddddd solid;">
- <div id="Div2" style="line-height:25px;text-align:center;background:#f00">
- 这个用自标签操作自己
- </div>
- 调用方法<br />var j=new Endrag('Div1','Div1',0,0);<br />
- 第一个参数为源标签对象或对象id属性;第二个参数为要移动的目标对象或对象id属性;第3、4个参数为偏移值,供最后调整用;<br />
- 鼠标指针在Div1标签内就有效
- </div>
- </body>
- </html>
实现方法二:
调用方法
Endrag.bind('MoveDiv').bind('Div1').offSet({x:0,y:0});
通过多次调用bind方法可以绑定任意多个Div标签;
offSet设置偏移,默认x偏移和y偏移都为0,供调整使用
本方法不能通过子元素绑定操作父元素
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title> 鼠标拖动层(可任意绑定DIV标签)(二) </title>
- <script language="javascript" type="text/javascript">
- /*
- 鼠标拖动层(可任意绑定DIV标签)(一)
- 作者:Anlige
- 联系方式:1034555083/zhanghuiguoanlige@126.com
-
- 调用方法:Endrag.bind('MoveDiv').bind('Div1').offSet({x:0,y:0});
- 参数说明:bind的参数为Div标签的ID属性或者Div对象本身;offSet的参数为偏移量,默认(不调用本方法)x,y偏移都为0
- 说明:通过多次调用bind方法绑定多个对象的拖动
- */
- Endrag={
- x0:0,y0:0,x1:0,y1:0,moveable:false,index:100,NS:(navigator.appName=='Netscape'),offSets:{x:0,y:0},
- //开始拖动;
- startDrag:function (e){
- e = e ? e : (window.event ? window.event : null);
- Endrag.getFocus(this);
- if(e.button==(Endrag.NS)?0 :1) {
- if(!Endrag.NS){this.setCapture()}
- Endrag.x0 = e.clientX ;
- Endrag.y0 = e.clientY ;
- Endrag.x1 = parseInt(Endrag.abs(this).x);
- Endrag.y1 = parseInt(Endrag.abs(this).y);
- Endrag.moveable = true;
- }
- },
- //拖动;
- drag:function (e){
- e = e ? e : (window.event ? window.event : null);
- if(Endrag.moveable){
- this.style.left = (Endrag.x1 + e.clientX - Endrag.x0 - Endrag.offSets.x) + "px";
- this.style.top = (Endrag.y1 + e.clientY - Endrag.y0 - Endrag.offSets.y) + "px";
- }
- },
- //停止拖动;
- stopDrag:function (e){
- if(Endrag.moveable) {
- if(!Endrag.NS){this.releaseCapture();}
- Endrag.moveable = false;
- }
- },
- //获得焦点;
- getFocus:function (obj){
- if(obj.style.zIndex!=this.index){
- this.index += 2;
- var idx = this.index;
- obj.style.zIndex=idx;
- }
- },
- abs:function (element) {
- var result = { x: element.offsetLeft, y: element.offsetTop};
- element = element.offsetParent;
- while (element) {
- result.x += element.offsetLeft;
- result.y += element.offsetTop;
- element = element.offsetParent;
- }
- return result;
- },
- offSet:function(setting){
- this.offSets=setting;
- },
- bind:function(obj){
- obj=typeof(obj)=="object" ? obj:document.getElementById(obj);
- obj.onmousedown=Endrag.startDrag;
- obj.onmouseup=Endrag.stopDrag;
- obj.onmousemove=Endrag.drag;
- return this;
- }
- };
- </script>
- </head>
- <body onload="Endrag.bind('MoveDiv').bind('Div1').offSet({x:0,y:0});">
- <div id="MoveDiv" style="background:#ffffff;position:absolute;left:200px;top:50px;width:500px;height:200px;border:1px #dddddd solid;">
- <div id="hand" style="line-height:25px;text-align:center;background:#f00">
- 拖动层
- </div>
- 调用方法<br />
- Endrag.bind('MoveDiv').bind('Div1').offSet({x:0,y:0});<br />
- 可以绑定任意多个Div标签;<br />offSet设置偏移,默认x偏移和y偏移都为0,供调整使用<br />
- 本方法不能通过子元素绑定操作父元素
- </div>
- <div id="Div1" style="background:#ffffff;position:absolute;left:400px;top:300px;width:300px;height:200px;border:1px #dddddd solid;">
- <div id="Div2" style="line-height:25px;text-align:center;background:#f00">
- 拖动层
- </div>
-
- </div>
- </body>
- </html>