JavaScript implementation can drag the color and close the Layer window instance

Source: Internet
Author: User
Tags min

The examples in this article describe the way JavaScript implements drag-and-drop and close-layer windows. Share to everyone for your reference. The specific analysis is as follows:

This is based on the JAVASCRIPT+CSS implementation layer drag code, the difference is that it when dragging the window will change color, so that the operation experience better, you can run code to view the effect. It can also show/hide or turn off the open effect, and when it's okay, look at it.

?

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--32 33 34 35 36 37 38-39 40 41 42 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 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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-18 0 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209-210 <html> <head> <title> Drag window </title> <meta http-equiv= "Content-type" content= text/html; Charset=utf-8 "> <style type= ' text/css ' > <!--body{font-size:12px;} a:visited{text-decoration:none;color : Slategray;} A:hover{text-decoration:underline;color:slategray} A:link{text-decoration:none;color:slategray;}--> </ Style> <script language=jscript> <!--var x0=0,y0=0,x1=0,y1=0; var offx=6,offy=6; var Moveable=false; var hover= ' Orange ', normal= ' #336699 ';//color; var Index=10000;//z-index; Start dragging; function StartDrag (obj) {if (event.button==1) {//Lock title bar; Obj.setcapture ();//define object; var win = Obj.parentnode; var sha = win . nextSibling; Record mouse and layer position; x0 = Event.clientx; y0 = Event.clienty; X1 = parseint (win.style.left); Y1 = parseint (win.style.top); Record color; normal = Obj.style.backgroundColor; Change style; Obj.style.backgroundColor = hover; Win.style.borderColor = hover; Obj.nextSibling.style.color = hover; Sha.style.left = x1 + offx; Sha.style.top = y1 + OFfy; moveable = true; }//drag; function drag (obj) {if (moveable) {var win = Obj.parentnode; var sha = win.nextsibling; win.style.left = x1 + Event.clien tx-x0; Win.style.top = y1 + event.clienty-y0; Sha.style.left = parseint (win.style.left) + offx; Sha.style.top = parseint (win.style.top) + offy; }}//Stop dragging; function Stopdrag (obj) {if (moveable) {var win = Obj.parentnode; var sha = win.nextsibling; var msg = obj.nextsibling; wi N.style.bordercolor = normal; Obj.style.backgroundColor = normal; Msg.style.color = normal; Sha.style.left = Obj.parentNode.style.left; Sha.style.top = Obj.parentNode.style.top; Obj.releasecapture (); moveable = false; }//Get focus; function GetFocus (obj) {if (obj.style.zindex!=index) {index = index + 2; var idx = index; obj.style.zindex=idx; obj.nexts Ibling.style.zindex=idx-1; }//Minimized; function min (obj) {var win = obj.parentNode.parentNode var sha = win.nextsibling var tit = obj.parentnode var msg = Ti t.nextsibling; var flg = msg.style.display== "None";if (FLG) {win.style.height = parseint (msg.style.height) + parseint (tit.style.height) + 2*2; sha.style.height = Win.style. Height Msg.style.display = "block"; obj.innerhtml = "0"; else {win.style.height = parseint (tit.style.height) + 2*2; sha.style.height = win.style.height; obj.innerhtml = "2"; ms G.style.display = "None"; }//Create an object; function Xwin (id,w,h,l,t,tit,msg) {index = index+2; this.id = id; this.width = w; this.height = h; this.left = l; this.to p = t; This.zindex = index; This.title = tit; This.message = msg; This.obj = null; This.bulid = Bulid; This.bulid (); }//initialization; function Bulid () {var str = "" + "<div id=xmsg" + this.id + "" + "style=" "+" Z-index: "+ This.zindex +"; "+" width: "+ This.width +"; "+" Height: "+ this.height +"; "+" Left: "+ This.left +"; "+" top: "+ This.top +"; "+" background-c Olor: "+ normal +"; "+" color: "+ normal +"; "+" font-size:8pt; "+" font-family:tahoma; "+" position:absolute; "+" curso R:default "+" border:2px solid "+ normal +";" + "'" + "onmousedown= ' GetFocus (this) ' >" + "<div" + "style= '" + "Background-color:" + normal + ";" + "width:" + (this.width-2*2) + ";" + "height:20" + "color:white;" + "'" + "onmousedown= ' StartDrag (This) '" + "onmouseup= ' Stopdrag (This) '" + "onmousemove=" Drag (This) ' "+" ondblclick= ' min (this.childnodes[1]) ' "+" > "+" <span style= ' width: + (this.width-2*12-4) + ";p Addi ng-left:3px; ' > "+ this.title +" </span> "+" <span style= ' width:12;border-width:0px;color:white;font-family:webdings; onclick= ' min (this) ' >0</span> ' + ' <span style= ' width:12;border-width:0px;color:white;font-family: Webdings ' onclick= ' showhide ("+this.id+" ", null) ' >r</span>" + "</div>" + "<div style=" "+" width:100 %; "+" Height: "+ (this.height-20-4) +"; "+" background-color:white; "+" line-height:14px; "+" word-break:break-all; "+" padding:3px "+" ' > "+ this.message +" </div> "+" </div> "+" <div id=xmsg "+ this.id +" bg style= ' "+" W Idth: "+ tHis.width + ";" + "Height:" + this.height + ";" + "top:" + This.top + "," + "Left:" + This.left + ";" + "Z-index:" + [this ZINDEX-1) + ";" + "position:absolute;" + "background-color:black;" + "Filter:alpha (opacity=40);" + "' ></div>"; Document.body.insertAdjacentHTML ("BeforeEnd", str); }//Show hidden window function Showhide (id,dis) {var bdisplay = (dis==null)? ( (document.getElementById ("Xmsg" +id). style.display== "")? None ":"):d is document.getElementById ("Xmsg" +id). style.display = Bdisplay; document.getElementById ("xmsg" +id+ "BG"). style.display = Bdisplay; }//--> </script> <script language= ' JScript ' > <!--function Initialize () {var a = new Xwin ("1", 160,200, 200,200, "Window 1", "Xwin Demo"); var B = new Xwin ("2", 240,200,100,100, "Window 2", "Welcome"); var c = new Xwin ("3", 200,160,250, 50, "Window 3", "Copyright"); Showhide ("1", "none");/hidden window 1} window.onload = Initialize; --> </script> </head> <base target= "_blank" > <body onselectstart= ' return false ' Oncontextmenu= ' reTurn false ' > <a onclick= ' showhide (' 1 ', null); return false; "href=" >windows 1</a> <a onclick= " Showhide (' 2 ', null); return false; "href=" ">windows 2</a> <a onclick=" showhide (' 3 ', null); Href= "" ">windows 3</a> </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.