2024年9月如何用JS实现文件拖拽事件?js如何实现层在指定区域内拖拽

 更新时间:2024-09-21 09:41:02

  ⑴如何用JS实现文件拖拽事件?js如何实现层在指定区域内拖拽

  ⑵是因为把部分信息保存在cookie中,大体思路就是选中div到某个位置后,工作中也用到了拖拉上传,本文目录如何用JS实现文件拖拽事件js如何实现层在指定区域内拖拽关于js拖拽如何用JS实现文件拖拽事件在HTML的pc上面实现了相当多的功能,在创建一个,很复杂,释放鼠标键时触发此事件js如何实现层在指定区域内拖拽《!DOCTYPEhtml》《html》《head》《metacharset="UTF-"》《title》拖拽《/title》《styletype="text/css"》*{margin:;padding:;}#box{width:px;height:px;border:pxsolidblack;}#block{width:px;height:px;background:red;position:absolute;left:px;top:px;}《/style》《/head》《body》《divid="box"》《divid="block"》《/div》《/div》《scripttype="text/javascript"》//获得元素varbox=document.getElementById(’box’);varblock=document.getElementById(’block’);functiongetCss(elmen,css){returnparseInt(window.getputedStyle?window.getputedStyle(elmen,null));}block.onmousedown=function(event){event=window.event||eventvarself=thisvarx=event.clientX-self.offsetLeft;vary=event.clientY-self.offsetTop;box.onmousemove=function(event){varmoveX=event.clientX-x;varmoveY=event.clientY-y;varboxSize=getCss(box,’width’);varblockSize=getCss(block,’width’)if(moveX》=(boxSize-blockSize)){moveX=boxSize-blockSize}if(moveX《=){moveX=}if(moveY《){moveY=}if(moveY》=(boxSize-blockSize)){moveY=boxSize-blockSize}block.style.left=moveX+’px’;block.style.top=moveY+’px’;}}block.onmouseup=function(){box.onmousemove=null}《/script》《/body》《/html》关于js拖拽vardragable={oldObj:null,obj:null,moveObjName:null,isBoxDragable:false,tx:,ty:,tx:,ty:,arrayTemplateLayout:null,arrayTemplateStyle:null,mapTemplateObject:newMap(),contextId:"context",objectId:"",ifremeObj:null,dragbox:null,dragboxId:"dragbox",oupation:"oupation",isImageObject:false,getDragBox:function(){if(!this.dragbox){this.dragbox=hs.createElement(’div’,{id:’’},null);this.dragbox.innerHTML=’《divid="dragbox"style="visibility:hidden;position:absolute;left:px;top:px;background:#ffffff;width:px;height:px"》《/div》’;document.body.appendChild(this.dragbox);}returnthis.dragbox.firstChild;},loadTemplateInfo:function(){this.arrayTemplateLayout=newArray();this.arrayTemplateStyle=newArray();this.searchObjectTag(document.getElementById(this.contextId));},makeRateBoxBarHandler:function(){try{vare=dragable.makeRateBoxBarHandler.arguments;varobj=dragable.makeRateBoxBarHandler.arguments;vartmpobj=obj;while(tmpobj.id.indexOf("_fordrag")==-&&!this.isBoxDragable)tmpobj=tmpobj.parentNode;if(!this.isBoxDragable&&!tmpobj){BoxUtils.makeRateBoxHandler(e,obj);returnfalse;}else{this.obj=tmpobj;if(!this.isBoxDragable)this.objectId=tmpobj.attributes.getNamedItem("objectid")?tmpobj.attributes.getNamedItem("objectid").nodeValue:"";}}catch(e){BoxUtils.makeRateBoxHandler(e,obj);returnfalse;}//ele.abc=;if(e.type==’mousedown’){if(this.isBoxDragable){this.rateBoxBarMouseUp(e);returnfalse;}hs.addEventListener(document,’mousemove’,this.rateBoxBarMouseMoveHandler);}elseif(e.type==’mouseup’){if(this.isBoxDragable)this.rateBoxBarMouseUp(e);elsehs.removeEventListener(document,"mousemove",this.rateBoxBarMouseMoveHandler);}e=e||window.event;if(e.preventDefault==null){e.preventDefault=function(){this.returnValue=false;};}if(e.stopPropagation==null){e.stopPropagation=function(){this.returnValue=false;};}e.preventDefault();Event.stop(e);},rateBoxBarMouseMoveHandler:function(e){if(dragable.isBoxDragable){dragable.getDragBox().style.cursor="move";dragable.getDragBox().style.left=(dragable.tx-dragable.tx+pointeurX(e))+"px";dragable.getDragBox().style.top=(dragable.ty-dragable.ty+pointeurY(e))+"px";varx=parseInt(dragable.getDragBox().style.left);//pointeurX(e);vary=parseInt(dragable.getDragBox().style.top);//pointeurY(e);if(dragable.isTemplateObject){varnodes=dragable.oldObj.parentNode.childNodes;for(vara=;a《nodes.length;a++){if(nodes.tagName!="DIV")continue;if(nodes==dragable.oldObj)continue;varp=hs.position(nodes);if(x》p.x&&y》p.y&&x《(p.x+nodes.offsetHeight)){nodes.firstChild.offsetWidth+"px";nodes.firstChild.offsetHeight+"px";dragable.oldObj.innerHTML=nodes.innerHTML;dragable.oldObj.style.border="";dragable.oldObj.style.width=dragable.oldObj.firstChild.offsetWidth+"px";dragable.oldObj.style.height=dragable.oldObj.firstChild.offsetHeight+"px";nodes.innerHTML="?";nodes.style.border="pxsolid#";//if(nodes.attributes.getNamedItem("index")){//vart=dragable.oldObj.attributes.getNamedItem("index").nodeValue;//dragable.oldObj.attributes.getNamedItem("index").nodeValue=//nodes.attributes.getNamedItem("index").nodeValue;//nodes.attributes.getNamedItem("index").nodeValue=t;//}dragable.oldObj=nodes;}}}}else{dragable.rateBoxBarInit_(e);}e=e||window.event;if(e.preventDefault==null){e.preventDefault=function(){this.returnValue=false;};}if(e.stopPropagation==null){e.stopPropagation=function(){this.returnValue=false;};}e.preventDefault();},findPlugin:function(obj){varlist=this.mapTemplateObject.getKeyList();for(vari=;i《list.length;i++){if(list.value.pareObject(obj)!=-)returnlist.value;}return-;},rateBoxBarMouseUp:function(e){//varele=document.getElementsByName("object");varx=parseInt(dragable.getDragBox().style.left);//pointeurX(e);vary=parseInt(dragable.getDragBox().style.top);//pointeurY(e);varele=this.arrayTemplateLayout;if(ele==null){dragable.loadTemplateInfo();ele=this.arrayTemplateLayout;}if(dragable.ifremeObj!=null&&dragable.isImageObject){varp=hs.position(dragable.ifremeObj);if(x》p.x&&y》p.y&&x《(p.x+this.ifremeObj.offsetWidth)&&y《(p.y+this.ifremeObj.offsetHeight)){bus_design.isInsertImage(null,dragable.getDragBox().firstChild.src);dragable.getDragBox().innerHTML="?";dragable.isBoxDragable=false;hs.removeEventListener(document,"mousemove",this.rateBoxBarMouseMoveHandler);dragable.getDragBox().style.visibility="hidden";return;}}for(vari=ele.length-;i》=;i--){//if(ele.attributes.getNamedItem("objects").nodeValue==dragable.oupation)//continue;if(ele==null)continue;varp=hs.position(ele);//document.getElementById("memo").innerHTML=x+"/"+y+"/"+p.x+"/"+p.y+"/"+(p.x+ele.offsetHeight);if(x》p.x&&y》p.y&&x《(p.x+ele.offsetHeight)){//ele.innerHTML="dragintoOK...";try{if(ele.firstChild.attributes.getNamedItem("name").nodeValue.indexOf("HtmlEdit")!=-){if(ele.firstChild.tagName=="IFRAME"){if(dragable.isImageObject)bus_design.isInsertImage(ele,dragable.getDragBox().firstChild.src);}elseif(!dragable.isImageObject)if(window.confirm(’偙咱僆僽仆僃僋僩傪徚偟偰怴偟偄僆僽仆僃僋僩傪擖傟偰傛傠偟偄偱偟效偆偐丠’)==){dragable.findPlugin(ele);if(dragable.mapTemplateObject.get(dragable.objectId)){dragable.mapTemplateObject.get(dragable.objectId).init(ele);}}}elseif(ele.firstChild.tagName=="IMG"){if(window.confirm(’偙咱僆僽仆僃僋僩傪徚偟偰怴偟偄僆僽仆僃僋僩傪擖傟偰傛傠偟偄偱偟效偆偐丠’)==){dragable.findPlugin(ele);if(dragable.mapTemplateObject.get(dragable.objectId)){dragable.mapTemplateObject.get(dragable.objectId).init(ele);}}}}catch(e){if(dragable.mapTemplateObject.get(dragable.objectId)){dragable.mapTemplateObject.get(dragable.objectId).init(ele);}}break;}}if(this.isTemplateObject){dragable.oldObj.style.border="";dragable.oldObj.innerHTML=dragable.getDragBox().innerHTML;dragable.oldObj.style.width=dragable.oldObj.firstChild.offsetWidth+"px";dragable.oldObj.style.height=dragable.oldObj.firstChild.offsetHeight+"px";dragable.isTemplateObject=false;bus_stack.refreshArray();}dragable.getDragBox().innerHTML="?";dragable.isBoxDragable=false;hs.removeEventListener(document,"mousemove",this.rateBoxBarMouseMoveHandler);dragable.getDragBox().style.visibility="hidden";},rateBoxBarInit_:function(e){vartmpBarHtml=newArray();try{this.isImageObject=false;this.isTemplateObject=false;if(this.obj.id.indexOf("img_")!=-)this.isImageObject=true;elseif(this.obj.id.indexOf("template_")!=-)this.isTemplateObject=true;this.getDragBox().className=this.obj.className;this.getDragBox().innerHTML=this.obj.innerHTML;this.getDragBox().style.width=this.obj.offsetWidth+"px";this.getDragBox().style.height=this.obj.offsetHeight+"px";this.getDragBox().style.zIndex=;if(this.isTemplateObject){this.obj.style.border="pxsolid#";this.obj.style.width=this.obj.firstChild.offsetWidth+"px";this.obj.style.height=this.obj.firstChild.offsetHeight+"px";this.obj.innerHTML="?";}varp=hs.position(this.obj);if(!hs.ie){p.y-=this.obj.parentNode.scrollTop;p.x-=this.obj.parentNode.scrollLeft;p.y-=document.body.scrollTop;p.x-=document.body.scrollLeft;}this.getDragBox().style.left=p.x+"px";this.getDragBox().style.top=p.y+"px";this.getDragBox().style.visibility="visible";this.tx=p.x;this.ty=p.y;this.tx=this.tx?this.tx:;this.ty=this.tx?this.ty:;this.tx=(pointeurX(e));this.ty=(pointeurY(e));this.isBoxDragable=true;this.oldObj=this.obj;}catch(e){hs.removeEventListener(document,"mousemove",this.rateBoxBarMouseMoveHandler);this.isBoxDragable=false;}},searchObjectTag:function(obj){if(obj!=null&&obj.childNodes!=null){varele=obj.childNodes;for(vari=;i《ele.length;i++){try{if(ele.attributes.getNamedItem("objects")!=null){this.arrayTemplateLayout;this.arrayTemplateStyle.style.border;}this.searchObjectTag(ele);}catch(e){}}}}};varBoxUtils={obj:null,tx:,ty:,tx:,ty:,makeRateBoxHandler:function(e,obj){if(obj.style.cursor!="move")return;try{while(obj.id.indexOf("_formove")==-&&obj.name!="closeImage")obj=obj.parentNode;}catch(e){returnfalse;}if(obj.name=="closeImage")returnfalse;this.obj=obj;//this.mapkey=root.attributes.getNamedItem("name").nodeValue;if(e.type==’mousedown’){varp=hs.position(obj);this.tx=p.xthis.ty=p.ythis.tx=this.tx?this.tx:;this.ty=this.tx?this.ty:;this.tx=pointeurX(e);this.ty=pointeurY(e);hs.addEventListener(document,’mousemove’,this.boxMouseMoveHandler);}elseif(e.type==’mouseup’){hs.removeEventListener(document,"mousemove",this.boxMouseMoveHandler)}e=e||window.event;if(e.preventDefault==null){e.preventDefault=function(){this.returnValue=false;};}if(e.stopPropagation==null){e.stopPropagation=function(){this.returnValue=false;};}e.preventDefault();},boxMouseMoveHandler:function(e){//document.getElementById(RateUtils.moveObjName).style.cursor="move";BoxUtils.obj.style.left=(BoxUtils.tx-BoxUtils.tx+pointeurX(e))+"px";BoxUtils.obj.style.top=(BoxUtils.ty-BoxUtils.ty+pointeurY(e))+"px";//BoxUtils.obj.style.zIndex=;//if(hs.ie)//setBackiframe(parseInt(BoxUtils.obj.style.left),parseInt(BoxUtils.obj.style.top),//BoxUtils.obj.firstChild.offsetWidth,BoxUtils.obj.firstChild.offsetHeight,,true);e=e||window.event;if(e.preventDefault==null){e.preventDefault=function(){this.returnValue=false;};}if(e.stopPropagation==null){e.stopPropagation=function(){this.returnValue=false;};}e.preventDefault();}};functioncreateDiv(html,key,visible){if(document.getElementById(’dragDiv_’+key))return;vardiv=hs.createElement(’div’,{id:’dragDiv_’+key},null);div.innerHTML=html;document.body.appendChild(div);returndiv;}这是个真实项目中的拖拽代码,特地记录下该功能在拖动目标上触发事件(源元素):ondragstart-用户开始拖动元素时触发ondrag-元素正在拖动时触发ondragend-用户完成元素拖动后触发释放目标时触发的事件:ondragenter-当被鼠标拖动的对象进入其容器范围内时触发此事件ondragover-当某被拖动的对象在另一对象容器范围内拖动时触发此事件ondragleave-当被鼠标拖动的对象离开其容器范围内时触发此事件ondrop-在一个拖动过程中。

  ⑶如何用JS实现文件拖拽事件

  ⑷在HTML的pc上面实现了相当多的功能,工作中也用到了拖拉上传,特地记录下该功能在拖动目标上触发事件(源元素):ondragstart-用户开始拖动元素时触发ondrag-元素正在拖动时触发ondragend-用户完成元素拖动后触发释放目标时触发的事件:ondragenter-当被鼠标拖动的对象进入其容器范围内时触发此事件ondragover-当某被拖动的对象在另一对象容器范围内拖动时触发此事件ondragleave-当被鼠标拖动的对象离开其容器范围内时触发此事件ondrop-在一个拖动过程中,释放鼠标键时触发此事件

  ⑸js如何实现层在指定区域内拖拽

  ⑹《!DOCTYPEhtml》《html》《head》《metacharset="UTF-"》《title》拖拽《/title》《styletype="text/css"》*{margin:;padding:;}#box{width:px;height:px;border:pxsolidblack;}#block{width:px;height:px;background:red;position:absolute;left:px;top:px;}《/style》《/head》《body》《divid="box"》《divid="block"》《/div》《/div》《scripttype="text/javascript"》//获得元素varbox=document.getElementById(’box’);varblock=document.getElementById(’block’);functiongetCss(elmen,css){returnparseInt(window.getputedStyle?window.getputedStyle(elmen,null));}block.onmousedown=function(event){event=window.event||eventvarself=thisvarx=event.clientX-self.offsetLeft;vary=event.clientY-self.offsetTop;box.onmousemove=function(event){varmoveX=event.clientX-x;varmoveY=event.clientY-y;varboxSize=getCss(box,’width’);varblockSize=getCss(block,’width’)if(moveX》=(boxSize-blockSize)){moveX=boxSize-blockSize}if(moveX《=){moveX=}if(moveY《){moveY=}if(moveY》=(boxSize-blockSize)){moveY=boxSize-blockSize}block.style.left=moveX+’px’;block.style.top=moveY+’px’;}}block.onmouseup=function(){box.onmousemove=null}《/script》《/body》《/html》

  ⑺vardragable={oldObj:null,obj:null,moveObjName:null,isBoxDragable:false,tx:,ty:,tx:,ty:,arrayTemplateLayout:null,arrayTemplateStyle:null,mapTemplateObject:newMap(),contextId:"context",objectId:"",ifremeObj:null,dragbox:null,dragboxId:"dragbox",oupation:"oupation",isImageObject:false,getDragBox:function(){if(!this.dragbox){this.dragbox=hs.createElement(’div’,{id:’’},null);this.dragbox.innerHTML=’《divid="dragbox"style="visibility:hidden;position:absolute;left:px;top:px;background:#ffffff;width:px;height:px"》《/div》’;document.body.appendChild(this.dragbox);}returnthis.dragbox.firstChild;},loadTemplateInfo:function(){this.arrayTemplateLayout=newArray();this.arrayTemplateStyle=newArray();this.searchObjectTag(document.getElementById(this.contextId));},makeRateBoxBarHandler:function(){try{vare=dragable.makeRateBoxBarHandler.arguments;varobj=dragable.makeRateBoxBarHandler.arguments;vartmpobj=obj;while(tmpobj.id.indexOf("_fordrag")==-&&!this.isBoxDragable)tmpobj=tmpobj.parentNode;if(!this.isBoxDragable&&!tmpobj){BoxUtils.makeRateBoxHandler(e,obj);returnfalse;}else{this.obj=tmpobj;if(!this.isBoxDragable)this.objectId=tmpobj.attributes.getNamedItem("objectid")?tmpobj.attributes.getNamedItem("objectid").nodeValue:"";}}catch(e){BoxUtils.makeRateBoxHandler(e,obj);returnfalse;}//ele.abc=;if(e.type==’mousedown’){if(this.isBoxDragable){this.rateBoxBarMouseUp(e);returnfalse;}hs.addEventListener(document,’mousemove’,this.rateBoxBarMouseMoveHandler);}elseif(e.type==’mouseup’){if(this.isBoxDragable)this.rateBoxBarMouseUp(e);elsehs.removeEventListener(document,"mousemove",this.rateBoxBarMouseMoveHandler);}e=e||window.event;if(e.preventDefault==null){e.preventDefault=function(){this.returnValue=false;};}if(e.stopPropagation==null){e.stopPropagation=function(){this.returnValue=false;};}e.preventDefault();Event.stop(e);},rateBoxBarMouseMoveHandler:function(e){if(dragable.isBoxDragable){dragable.getDragBox().style.cursor="move";dragable.getDragBox().style.left=(dragable.tx-dragable.tx+pointeurX(e))+"px";dragable.getDragBox().style.top=(dragable.ty-dragable.ty+pointeurY(e))+"px";varx=parseInt(dragable.getDragBox().style.left);//pointeurX(e);vary=parseInt(dragable.getDragBox().style.top);//pointeurY(e);if(dragable.isTemplateObject){varnodes=dragable.oldObj.parentNode.childNodes;for(vara=;a《nodes.length;a++){if(nodes.tagName!="DIV")continue;if(nodes==dragable.oldObj)continue;varp=hs.position(nodes);if(x》p.x&&y》p.y&&x《(p.x+nodes.offsetHeight)){nodes.firstChild.offsetWidth+"px";nodes.firstChild.offsetHeight+"px";dragable.oldObj.innerHTML=nodes.innerHTML;dragable.oldObj.style.border="";dragable.oldObj.style.width=dragable.oldObj.firstChild.offsetWidth+"px";dragable.oldObj.style.height=dragable.oldObj.firstChild.offsetHeight+"px";nodes.innerHTML="?";nodes.style.border="pxsolid#";//if(nodes.attributes.getNamedItem("index")){//vart=dragable.oldObj.attributes.getNamedItem("index").nodeValue;//dragable.oldObj.attributes.getNamedItem("index").nodeValue=//nodes.attributes.getNamedItem("index").nodeValue;//nodes.attributes.getNamedItem("index").nodeValue=t;//}dragable.oldObj=nodes;}}}}else{dragable.rateBoxBarInit_(e);}e=e||window.event;if(e.preventDefault==null){e.preventDefault=function(){this.returnValue=false;};}if(e.stopPropagation==null){e.stopPropagation=function(){this.returnValue=false;};}e.preventDefault();},findPlugin:function(obj){varlist=this.mapTemplateObject.getKeyList();for(vari=;i《list.length;i++){if(list.value.pareObject(obj)!=-)returnlist.value;}return-;},rateBoxBarMouseUp:function(e){//varele=document.getElementsByName("object");varx=parseInt(dragable.getDragBox().style.left);//pointeurX(e);vary=parseInt(dragable.getDragBox().style.top);//pointeurY(e);varele=this.arrayTemplateLayout;if(ele==null){dragable.loadTemplateInfo();ele=this.arrayTemplateLayout;}if(dragable.ifremeObj!=null&&dragable.isImageObject){varp=hs.position(dragable.ifremeObj);if(x》p.x&&y》p.y&&x《(p.x+this.ifremeObj.offsetWidth)&&y《(p.y+this.ifremeObj.offsetHeight)){bus_design.isInsertImage(null,dragable.getDragBox().firstChild.src);dragable.getDragBox().innerHTML="?";dragable.isBoxDragable=false;hs.removeEventListener(document,"mousemove",this.rateBoxBarMouseMoveHandler);dragable.getDragBox().style.visibility="hidden";return;}}for(vari=ele.length-;i》=;i--){//if(ele.attributes.getNamedItem("objects").nodeValue==dragable.oupation)//continue;if(ele==null)continue;varp=hs.position(ele);//document.getElementById("memo").innerHTML=x+"/"+y+"/"+p.x+"/"+p.y+"/"+(p.x+ele.offsetHeight);if(x》p.x&&y》p.y&&x《(p.x+ele.offsetHeight)){//ele.innerHTML="dragintoOK...";try{if(ele.firstChild.attributes.getNamedItem("name").nodeValue.indexOf("HtmlEdit")!=-){if(ele.firstChild.tagName=="IFRAME"){if(dragable.isImageObject)bus_design.isInsertImage(ele,dragable.getDragBox().firstChild.src);}elseif(!dragable.isImageObject)if(window.confirm(’偙咱僆僽仆僃僋僩傪徚偟偰怴偟偄僆僽仆僃僋僩傪擖傟偰傛傠偟偄偱偟效偆偐丠’)==){dragable.findPlugin(ele);if(dragable.mapTemplateObject.get(dragable.objectId)){dragable.mapTemplateObject.get(dragable.objectId).init(ele);}}}elseif(ele.firstChild.tagName=="IMG"){if(window.confirm(’偙咱僆僽仆僃僋僩傪徚偟偰怴偟偄僆僽仆僃僋僩傪擖傟偰傛傠偟偄偱偟效偆偐丠’)==){dragable.findPlugin(ele);if(dragable.mapTemplateObject.get(dragable.objectId)){dragable.mapTemplateObject.get(dragable.objectId).init(ele);}}}}catch(e){if(dragable.mapTemplateObject.get(dragable.objectId)){dragable.mapTemplateObject.get(dragable.objectId).init(ele);}}break;}}if(this.isTemplateObject){dragable.oldObj.style.border="";dragable.oldObj.innerHTML=dragable.getDragBox().innerHTML;dragable.oldObj.style.width=dragable.oldObj.firstChild.offsetWidth+"px";dragable.oldObj.style.height=dragable.oldObj.firstChild.offsetHeight+"px";dragable.isTemplateObject=false;bus_stack.refreshArray();}dragable.getDragBox().innerHTML="?";dragable.isBoxDragable=false;hs.removeEventListener(document,"mousemove",this.rateBoxBarMouseMoveHandler);dragable.getDragBox().style.visibility="hidden";},rateBoxBarInit_:function(e){vartmpBarHtml=newArray();try{this.isImageObject=false;this.isTemplateObject=false;if(this.obj.id.indexOf("img_")!=-)this.isImageObject=true;elseif(this.obj.id.indexOf("template_")!=-)this.isTemplateObject=true;this.getDragBox().className=this.obj.className;this.getDragBox().innerHTML=this.obj.innerHTML;this.getDragBox().style.width=this.obj.offsetWidth+"px";this.getDragBox().style.height=this.obj.offsetHeight+"px";this.getDragBox().style.zIndex=;if(this.isTemplateObject){this.obj.style.border="pxsolid#";this.obj.style.width=this.obj.firstChild.offsetWidth+"px";this.obj.style.height=this.obj.firstChild.offsetHeight+"px";this.obj.innerHTML="?";}varp=hs.position(this.obj);if(!hs.ie){p.y-=this.obj.parentNode.scrollTop;p.x-=this.obj.parentNode.scrollLeft;p.y-=document.body.scrollTop;p.x-=document.body.scrollLeft;}this.getDragBox().style.left=p.x+"px";this.getDragBox().style.top=p.y+"px";this.getDragBox().style.visibility="visible";this.tx=p.x;this.ty=p.y;this.tx=this.tx?this.tx:;this.ty=this.tx?this.ty:;this.tx=(pointeurX(e));this.ty=(pointeurY(e));this.isBoxDragable=true;this.oldObj=this.obj;}catch(e){hs.removeEventListener(document,"mousemove",this.rateBoxBarMouseMoveHandler);this.isBoxDragable=false;}},searchObjectTag:function(obj){if(obj!=null&&obj.childNodes!=null){varele=obj.childNodes;for(vari=;i《ele.length;i++){try{if(ele.attributes.getNamedItem("objects")!=null){this.arrayTemplateLayout;this.arrayTemplateStyle.style.border;}this.searchObjectTag(ele);}catch(e){}}}}};varBoxUtils={obj:null,tx:,ty:,tx:,ty:,makeRateBoxHandler:function(e,obj){if(obj.style.cursor!="move")return;try{while(obj.id.indexOf("_formove")==-&&obj.name!="closeImage")obj=obj.parentNode;}catch(e){returnfalse;}if(obj.name=="closeImage")returnfalse;this.obj=obj;//this.mapkey=root.attributes.getNamedItem("name").nodeValue;if(e.type==’mousedown’){varp=hs.position(obj);this.tx=p.xthis.ty=p.ythis.tx=this.tx?this.tx:;this.ty=this.tx?this.ty:;this.tx=pointeurX(e);this.ty=pointeurY(e);hs.addEventListener(document,’mousemove’,this.boxMouseMoveHandler);}elseif(e.type==’mouseup’){hs.removeEventListener(document,"mousemove",this.boxMouseMoveHandler)}e=e||window.event;if(e.preventDefault==null){e.preventDefault=function(){this.returnValue=false;};}if(e.stopPropagation==null){e.stopPropagation=function(){this.returnValue=false;};}e.preventDefault();},boxMouseMoveHandler:function(e){//document.getElementById(RateUtils.moveObjName).style.cursor="move";BoxUtils.obj.style.left=(BoxUtils.tx-BoxUtils.tx+pointeurX(e))+"px";BoxUtils.obj.style.top=(BoxUtils.ty-BoxUtils.ty+pointeurY(e))+"px";//BoxUtils.obj.style.zIndex=;//if(hs.ie)//setBackiframe(parseInt(BoxUtils.obj.style.left),parseInt(BoxUtils.obj.style.top),//BoxUtils.obj.firstChild.offsetWidth,BoxUtils.obj.firstChild.offsetHeight,,true);e=e||window.event;if(e.preventDefault==null){e.preventDefault=function(){this.returnValue=false;};}if(e.stopPropagation==null){e.stopPropagation=function(){this.returnValue=false;};}e.preventDefault();}};functioncreateDiv(html,key,visible){if(document.getElementById(’dragDiv_’+key))return;vardiv=hs.createElement(’div’,{id:’dragDiv_’+key},null);div.innerHTML=html;document.body.appendChild(div);returndiv;}这是个真实项目中的拖拽代码。很复杂,大体思路就是选中div到某个位置后,在创建一个。至于能保存下来,是因为把部分信息保存在cookie中。

您可能感兴趣的文章:

相关文章