JavaScript to implement DIV-layer drag and dynamically add new layer method

Source: Internet
Author: User

This article illustrates the method of JavaScript implementing div-layer dragging and dynamically adding new layer. Share to everyone for your reference. The specific analysis is as follows:

No refresh adds a new div layer, and can be implemented by the drag layer, drag the mouse to move the position, the JS part of the Save as a new file, when used from the external introduction, this drag layer code is very popular, GG and Yahoo and other large sites can often see this effect.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 <html> <head> <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/> <title>div layer drag, dynamically add new layer of javascript</title> <style> body {margin:0px;padding:0px ; font-size:12px;text-align:center;} Body > div {text-align:center; margin-right:auto; Margin-left:auto}. content{width:900px;} content. left{float: Left width:20%; border:1px solid #0066CC; margin:3px; center{float:left border:1px solid #FF0000; margin:3px; width:57%} content right{float:right; border:1px solid #FF0000; MARGIN:3PX}. mo{Height:auto border:1px solid #CCC margin:3px: #FFF}. MO background h1{: #ECF9FF; background X padding:3px; Cursor:move}. closediv{cursor:default; minusspan{cursor:default nr{; height:80px border:1px #F3F3F3 m ARGIN:2PX} h1{margin:0px padding:0px text-align:left font-size:12px} dragging {FILTER:progid:DXImageTransform.Mi Crosoft. Alpha (opacity=60); opacity:0.6; moz-opacity:0.6} </Style> <script language= "JavaScript" > var dragobj={} window.onerror=function () {return false} var domid=12 function On_ini () {string.prototype.inc=function (s) {return this.indexof (s) >-1?true:false} var agent= Navigator.useragent window.isopr=agent.inc ("Opera") window.isie=agent.inc ("IE") &&!isopr window.ismoz= Agent.inc ("Mozilla") &&!isopr &&!isie if (ismoz) {event.prototype.__definegetter__ ("X", function () { Return this.clientx+2}) event.prototype.__definegetter__ ("Y", function () {return this.clienty+2})} Basic_ini ()} function Basic_ini () {window.$=function (obj) {return typeof (obj) = "string"? document.getElementById (obj): obj} Window.odel=function (obj) {if ($ (obj)!=null) {$ (obj)}} window.odel=function (obj) { if ($ (obj)!=null) {$ (obj). Parentnode.removechild ($ (obj)}} window.onload=function () {On_ini () var o= document.getElementsByTagName ("H1") for (Var i=0;i<o.length;i++) {o[i].onmousedown=addevent;//Add collapse and Close button var TT = Document.crEateelement ("div"); Tt.style.cssText = "Float:left";   var span = document.createelement ("span"); span.innerhtml = "--" +o[i].innerhtml; Span.style.cssText = "cursor:default;"; Span.onmousedown = Minusdiv; Tt.appendchild (span);   var close = document.createelement ("div"); close.innerhtml = "X"; Close.style.cssText = "Cursor:default;float:right"; Close.onmousedown = Closediv; o[i].innerhtml = ""; O[i].appendchild (TT); O[i].appendchild (Close); }//collapse or display layer function Minusdiv (e) {e=e| | Event var nr = this.parentNode.parentNode.nextSibling; Get content Layer Nr.style.display = nr.style.display== ""? None ":" ";} Remove Layer function Closediv (e) {e=e| | event var mdiv = This.parentNode.parentNode; Obtaining the target layer Odel (MDIV); function Addevent (e) {if (Dragobj.o!=null) return False e=e| | Event Dragobj.o=this.parentnode Dragobj.xy=getxy (DRAGOBJ.O) dragobj.xx=new Array ((e.x-dragobj.xy[1)), ( E.y-dragobj.xy[0])//dragobj.o.classname = ' dragging '; dragobj.o.style.width=dragobj.xy[2]+ "px" Dragobj.o.style.height=dragobj.xy[3]+ "px" dragobj.o.style.left= (e.x-dragobj.xx[0]) + "px" dragobj.o.style.top= (e.y-dragobj.xx[1)) + "px" dragobj.o.style.position= "Absolute" dragobj.o.style.filter= ' alpha (opacity=60) '; Add drag transparency effect var om=document.createelement ("div") dragobj.otemp=om om.style.width=dragobj.xy[2]+ "px" om.style.height= dragobj.xy[3]+ "px" Om.style.border = "1px dashed red"; Ikaiser Add, implement dashed box Dragobj.o.parentnode.insertbefore (OM,DRAGOBJ.O) return false} document.onselectstart=function () { return false} window.onfocus=function () {document.onmouseup ()} window.onblur=function () {document.onmouseup ()} Document.onmouseup=function () {if (dragobj.o!=null) {dragobj.o.style.width= "auto" dragobj.o.style.height= "Auto" Dragobj.otemp.parentNode.insertBefore (dragobj.o,dragobj.otemp) dragobj.o.style.position= "" Odel (dragobj.otemp) dragobj={}} document.onmousemove=function (e) {e=e| | Event if (dragobj.o!=null) {dragobj.o.style.left= (e.x-dragobj.xx[0]) + "px" dragobj.o.style.top= (e.y-dragobj.xx[1)) + "px" Createtmpl (e, DRAGOBJ.O)//Pass the current drag object}} function Getxy (e) {var a=new Array () var t=e.offsettop; var L=e.offsetleft; var W=e.offsetwidth; var H=e.off SetHeight; while (e=e.offsetparent) {t+=e.offsettop; L+=e.offsetleft} a[0]=t;a[1]=l;a[2]=w;a[3]=h return A; function inner (o,e) {var a=getxy (o) if (e.x>a[1] && e.x< (a[1]+a[2)) && e.y>a[0] && e.y& lt; (A[0]+a[3])) {if (e.y< (A[0]+A[3]/2)) return 1; else return 2;} else return 0; //Change the current drag layer to size when dragging, preview effect function Createtmpl (e, Elm) {for (Var i=0;i<domid;i++) {if (document.getElementById ("M" +i) = = null)//The removed layer is no longer traversing the continue; if ($ ("M" +i) ==DRAGOBJ.O) continue Var B=inner ($ ("M" +i), E) if (b==0) Continue dragobj.otemp.style.width=$ ("M" +i). Offsetwidth Elm.style.width = $ ("M" +i). offsetwidth; 1 is down and 2 is up to (b==1) {$ ("M" +i). Parentnode.insertbefore (dragobj.otemp,$ ("M" +i))}else{if ($ ("M" +i). nextsibling== NULL) {$ ("M" +i). Parentnode.appendchild (dragobj.otemp)}else{$ ("M" +i). Parentnode.insertbefore (dragobj.otemp,$ ("M" +i). nextSibling)}}for (Var j=0;j<3;j++) {if ($ ("Dom" +j). Innerhtml.inc ("div") | | $ ("Dom" +j). Innerhtml.inc ("DIV")) continue Var op=getxy ($ ("dom" +j)) if (e.x> (op[1]+10) && e.x< (op[1]+op[ 2]-10)) {$ ("dom" +j). AppendChild (dragobj.otemp) dragobj.otemp.style.width= (op[2]-10) + "px"}}} function Add_div () {var O=document.createelement ("div") o.classname= "Mo" o.id= "M" +domid $ (' dom0 '). appendchild (o) o.innerhtml= "<h1> Dom "+domid+" </h1><div class=nr></div> o.getelementsbytagname ("H1") [0].onmousedown=addevent domid++} </script> </head> <body> <input type= "button" value= "adds a new div layer" onclick= "add_div (); > <div class=content> <div class=left id=dom0> <div class=mo id=m0> <h1>dom0</h1> < Div class= "nr" ></div> </div> <div class=mo id=m1> <h1>dom1</h1><div "NR" ></div> </div> <div class=mo id=m2><h1>dom2</h1><div class= "NR" ></div> </div> <Div class=mo id=m3><h1>dom3</h1><div class= "nr" ></div></div> </div> <div Class=center id=dom1> <div class=mo id=m4><h1>dom4</h1><div class= "NR" ></div></ div> <div class=mo id=m5><h1>dom5</h1><div class= "nr" ></div></div> </div > <div class=right id=dom2> <div class=mo id=m8><h1>dom8</h1><div class= "NR" ></ div></div> <div class=mo id=m9><h1>dom9</h1><div class= "NR" ></div></div > </div> </div> </body> </html>

The

wants this article to help you with your JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.