JavaScript to implement mouse drag to change the size of the layer method

Source: Internet
Author: User
Tags min

This paper illustrates how JavaScript implements the mouse drag to change the size of the layer. Share to everyone for your reference. The implementation methods are as follows:

?

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 <html> <head> <title> Drag to change the size of the layer </title> <meta content= "text/html; charset=gb2312 "http-equiv=" Content-type "> <style> {box-sizing:border-box; Moz-box-sizing:border-box} # testdiv {background-color:buttonface; background-repeat:repeat; background-attachment:scroll; color: #3969A5; height : 300px; left:30px; Overflow:hidden; width:500; Z-index:2; border:2px outset white; margin:0px; padding:2px; background-position:0% 50%} body {font-family:verdana; font-size:9pt} #innerNice {background-color:white; Backgrou Nd-repeat:repeat; Background-attachment:scroll; Color: #3969A5; height:100%; Overflow:auto; width:100%; border:2px inset White; padding:8px; background-position:0% 50%} </style> <script language=javascript> var theobject = null; function Resizeobject () {this.el = null;//pointer to the object this.dir = ""; Type of current resize (N,S,E,W,NE,NW,SE,SW) THIS.GRABX = null; Some useful values This.graby = nulL This.width = null; This.height = null; This.left = null; This.top = null; function Getdirection (EL) {var xpos, YPos, offset, dir; dir = ""; Xpos = Window.event.offsetX; YPos = Window.event.offsetY; offset = 8; The distance from the edge in pixels if (ypos<offset) dir = = "n"; else if (YPos > El.offsetheight-offset) dir = + "S"; if (xpos<offset) dir = = "W"; else if (xpos > El.offsetwidth-offset) dir = + "E"; return dir; function Dodown () {var el = getreal (event.srcelement, "ClassName", "resizeme"); if (el = = null) {theobject = null; RET Urn dir = Getdirection (EL); if (dir = = "") return; Theobject = new Resizeobject (); Theobject.el = El; Theobject.dir = dir; THEOBJECT.GRABX = Window.event.clientX; Theobject.graby = Window.event.clientY; Theobject.width = El.offsetwidth; Theobject.height = El.offsetheight; Theobject.left = El.offsetleft; Theobject.top = El.offsettop; Window.event.returnValue = false; Window.event.cancelBubble = true; The function Doup () {if (THEOBJEC)T!= null) {theobject = null;}} function Domove () {var el, xpos, YPos, str, xmin, ymin; xmin = 8;//the smallest width possible ymin = 8;//Height el = Getreal (event.srcelement, "ClassName", "Resizeme"); if (El.classname = = "Resizeme") {str = getdirection (EL);//fix the cursor if (str = "") str = "Default"; else str = "-resize"; El.style.cursor = str; }//dragging starts here if (theobject!= null) {if (Dir.indexof ("E")!=-1) theobject.el.style.width = Math.max (Xmin, the Object.width + WINDOW.EVENT.CLIENTX-THEOBJECT.GRABX) + "px"; if (Dir.indexof ("s")!=-1) theobject.el.style.height = Math.max (ymin, Theobject.height + window.event.clienty-theobjec T.graby) + "px"; if (Dir.indexof ("W")!=-1) {theobject.el.style.left = Math.min (Theobject.left + WINDOW.EVENT.CLIENTX-THEOBJECT.GRABX, Theobject.left + theobject.width-xmin) + "px"; Theobject.el.style.width = Math.max (xmin, Theobject.width-window.event.clientx + theobject.grabx) + "px"; } if (Dir.indexof ("n")!=-1) {TheoBject.el.style.top = math.min (Theobject.top + window.event.clienty-theobject.graby, Theobject.top + theobject.height- ymin) + "px"; Theobject.el.style.height = Math.max (ymin, Theobject.height-window.event.clienty + theobject.graby) + "px"; } Window.event.returnValue = false; Window.event.cancelBubble = true; } function Getreal (el, type, value) {temp = el; while (temp!= null) && (temp.tagname!= ' body ')) {if (eval () Temp. "+ type) = = value" {el = temp; return el;} temp = temp.parentelement; Return el; } Document.onmousedown = Dodown; Document.onmouseup = Doup; Document.onmousemove = Domove; </SCRIPT> </head> <body> <div class= "Resizeme" id= "Testdiv" > <div id= "Innernice" > <p align= "center" > </p> <p align= "center" > Please drag the mouse around the border </p> <p> </p> <p> </p> <p> </p> </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.