Drag and drop is a very useful page effect
1. Drag and drop some of the application scenarios:
1.1. Browser tab order switching
1.2. Drag and drop widgets on the page
1.3. Drag and drop the pop-up layer
2. Fundamentals
2.1 The basic principle of drag and drop is:
When the mouse is pressed on the element, it gets the position of the mouse on the page and calculates the position of the relative element of the mouse: Disx,disy;
When the mouse moves, the element moves with the mouse, but the mouse is not moving relative to the element, so the position of the element can be calculated according to Disx,disy;
When the mouse is released, the element stops moving;
The most important point is that the element that needs to be dragged position to be set to absolute or relative so that it can be moved.
2.2 Event: The first three steps above correspond to three events
OnMouseDown: triggered when the user presses any mouse button
OnMouseMove: repeatedly fires when the mouse pointer moves inside an element
OnMouseUp: triggers when the user releases the mouse button
3. An example
HTML code:
<id= "box"></div>
CSS code:
1 HTML,2 Body{3 width:100%;4 Height:100%;5}6 #box{7 width:100px;8 Height:100px;9 Background-color:Pink;Ten position:Absolute; One}
JS Code:
1 varbox = document.getElementById (' box ');2Box.onmousedown =function(){3 varevent = event?event:window.event;4 5 //gets the position of the mouse relative element6 varDISX = Event.clientx-Box.offsetleft;7 varDisy = Event.clienty-Box.offsettop;8 9 //gets the position of the element when the mouse movesTenDocument.onmousemove =function(){ One varevent = event?event:window.event; A varIL = Event.clientx-Disx; - varIT = Event.clienty-Disy; -Box.style.left = IL + ' px '; theBox.style.top = IT + ' px '; - } - - //when the mouse is released, the element stops with the mouse +Document.onmouseup =function(){ -Document.onmousemove =NULL; +Document.onmouseup =NULL; A } at -}
Increase the magnetic adsorption effect, JS code changed to:
1 varbox = document.getElementById (' box ');2Box.onmousedown =function(){3 varevent = event?event:window.event;4 5 //gets the position of the mouse relative element6 varDISX = Event.clientx-Box.offsetleft;7 varDisy = Event.clienty-Box.offsettop;8 9 //gets the position of the element when the mouse movesTenDocument.onmousemove =function(){ One varevent = event?event:window.event; A varIL = Event.clientx-Disx; - varIT = Event.clienty-Disy; - the //Magnetic Adsorption - if(il<=50){ -IL = 0; -}Else if(il>=document.documentelement.offsetwidth-box.offsetwidth-50){ +IL = document.documentelement.offsetwidth-Box.offsetwidth; - } + if(it<=50){ AIT = 0; at}Else if(it>=document.documentelement.offsetheight-box.offsetheight-50){ -IT = document.documentelement.offsetheight-Box.offsetheight; - } -Box.style.left = IL + ' px '; -Box.style.top = IT + ' px '; - } in - //when the mouse is released, the element stops with the mouse toDocument.onmouseup =function(){ +Document.onmousemove =NULL; -Document.onmouseup =NULL; the } *}
JS implementation of mouse drag-and-drop effect