This article mainly introduces the mouse to drag and drop the Mobile child form JS Implementation, the need for friends can refer to the
1. Subform When designing a Web site, we need to design some modal subforms, such as This step is easy to achieve, only need to div+css on the OK, see Code: Code as follows: <div class= "Modal-background" ></div > <div class= "Modal-window" > <div class= "Head" > &NB Sp <center> Point live block area can change my position </center> </div> & Lt;/div> Code as follows:. modal-background { Background-color: #999999 bottom:0; &NBSP ; left:0; opacity:0.3; position:fixed; right:0; top:0; z-index:1100; } Modal-window { Background-color: #FFFFFF border:1px solid #6B94AD; B ox-shadow:5px 5px 5px #6B94AD; font-family: ' Microsoft Yahei '; font-size:12px; height:120px; left:50%; Margin-left: -160px; Margin-top: -160px; OPACity:1; position:fixed; top:50%; width:320px; z-index:1110; } Modal-window. Head { height:25px; Color: #fff; font-weight:600; Background-image:-moz-linear-gradient (Top, #4A8CC5, #2963A5); * Firefox * Background-image:-webkit-gradient (linear, left top, left bottom, color-stop (0, #4A8CC5), Color-stop (1, #2963A5)); /* saf4+, Chrome/ filter:progid:DXImageTransform.Microsoft.gradient (startcolorstr= ' # 4a8cc5 ', endcolorstr= ' #2963A5 ', gradienttype= ' 0 '); * ie*/ } Modal-window. Head Center { padding-top:2px; } plus the HTML and CSS above can easily achieve the above modal form. of which left:50%;top:50%; Margin-left: -160px; Margin-top: -160px to achieve the center effect of this modal form. Of course, the size of the modal form is in the style class. Modal-window is fixed, this is not to say not to modify the size of modal forms, such as I write code: Code as follows: <div class= " Modal-background "></div> <div class=" Modal-window List-window "> <div class= "Head" > <center> Point live block area can change my position </center> & nbsp </div> </div> Appended in the second line of code. List-window this style class overrides the size and position in the. Modal-window class, but also ensures that the modal form is centered code as follows:. List-window { width:600px; height:400px; margin-left:-300px; margin-top:-200px; } as shown It can be seen that this step of the implementation is very simple, master several key lines of CSS properties on the "end of abuse" of this modal subform, all kinds of other modal subform can extrapolate slightly. 2. How do I implement the drag and drop of the subform when the mouse clicks on the head of the subform? When JQ is introduced, we need only a few scripts to handle this small feature. Don't believe us. code is as follows: Var left, top, $this; $ (document). Delegate (' Modal-window. Head ', ' MouseDown ', function (e) { left = e.clientx, top = E.clienty, $ This = $ (this). CSS (' cursor ', ' move '); This.setcapture? ( this.setcapture (), This.onmousemove = function (EV) {mouseMove (EV | |. event);}, &nbs P This.onmouseup = mouseUp : $ (document). Bind ("MouseMove", MouseMove). Bind ("MouseUp", mouseUp); }); function MouseMove (e) { var target = $this. Parents ('. Modal-window '); var L = Math.max ((e.clien Tx-left + number (target.css (' Margin-left '). Replace (/px$/, ')) | | 0),-target.position (). left); var t = Math.max (e.clienty-top + number (target.css (' Margin-top '). Replace (/px$/, ')) | | 0),-target.posit Ion (). top); L = MATH.MIN (L, $ (window). Width ()-target.width ()-target.position (). left); T = math.min (t, $ (window). Height ()-target.height ()-target.position (). top); left = E.clientx; top = E.clienty; TARGET.CSS ({' Margin-left ': L, ' margin-top ': t}); function MouseUp (e) { var el = $this. CSS (' cursor ', ' Default '). Get (0); el.releasecapture? &n Bsp ( el.releasecapture (), El.onmousemove = El.onmouseup = null ): $ (document). Unbind ("MouseMove", MouseMove). Unbind ("MouseUp", MouseUp); This code is very brief and can run smoothly in a variety of browsers. In fact, the principle of its implementation is very simple, roughly divided into three steps: ① when the mouse is under the modal form header (MouseDown), the document is immediately bound MouseMove and MouseUp events ② when the mouse is not bouncing ( No MouseUp), if the mouse in the window to move, activate the MouseMove function, by calculating the distance of the mouse movement to timely move the entire form position. ③ when the mouse is bouncing (MouseUp), the MouseUp event is invoked to unbind the MouseMove and MouseUp events bound by the document. The whole process of the principle is: when the mouse MouseDown, the mouse movement event transferred to the document, through the mouse on the document movement events to the entire form processing. In addition, there is a trick in MouseMove that the global left,top variable records the position of the last mouse stop, and then the next time the mouse position is compared to the left,top variable to determine how far the mouse is moving, To move the corresponding position to the entire modal subform. Through this code analysis, found that the mouse mobile form and even any element on the document is very easy For example, if you want to drag to change the size of the form, We just need to resize the form in the MouseMove event handler. OK, did you find that you have learned a trick and made a small step? Some people will ask SetCapture and releasecapture respectively what are they doing? In fact, this is to be compatible with IE, only IE have these two functions, in this contempt ie. SetCapture allows the current element to capture all of the mouse's events and may not be compatible with IE if you do not use them.