The drag function is mainly used to allow users to perform some customized actions, such as dragging to sort, dragging and moving pop-up boxes, etc. Drag and drop process actions1. Pressing the mouse will trigger the onmousedown event 2. Mouse movement triggers the onmousemove event 3. Releasing the mouse will trigger the onmouseup event Drag principle1. Mouse press + mouse move = drag-------event onmousedown + onmousemove 2. Release the mouse = no drag -------- stop dragging onmouseup 3. Mouse offset = drag distance When clicking on the DOM, record the current mouse coordinates, that is, the x and y values, as well as the top and left values of the dragged DOM, and add a mouse move event in the mouse pressed callback function: document.addEventListener("mousemove", moving, false) And add the mouse up event document.addEventListener("mouseup",function() { document.removeEventListener("mousemove", moving, false); }, false); This lift event is used to cancel the monitoring of mouse movement, because dragging can only be done when the mouse is pressed, and it will stop moving when it is lifted. When the mouse is pressed and moved, the x and y values of the movement are recorded, then the top and left values of the dragged DOM are: //Extremely simple version var div = document.querySelector("div"); // Start listening for mouse movement events in the document when pressed // Start listening for mouse release events // Only prepare to drag when pressed div.onmousedown=function(e1){ // When the mouse moves in the document, it cannot move on the div, because the mouse may leave the div, making it impossible to drag the div.onmousemove=function(e){ // When the mouse moves, assign the current mouse coordinates relative to the viewport to the left and top of the element // Because we need to drag at the pressed position, we also need to get the upper left corner position of the mouse relative to the div when the key is pressed. // Use the current mouse position minus the upper left corner position of the relative element to ensure that the mouse is dragged at the pressed position. div.style.left=e.clientX-e1.offsetX+"px"; div.style.top=e.clientY-e1.offsetY+"px"; } // When the mouse button is released, delete the mouse move event and delete the mouse release event div.onmouseup=function(){ document.onmousemove=null; document.onmouseup=null; } } //Simple version var div = document.querySelector("div"); var offsetX,offsetY; div.addEventListener("mousedown",mouseDownHandler); function mouseDownHandler(e){ offsetX=e.offsetX offsetY=e.offsetY document.addEventListener("mousemove",mousemoveHandler) document.addEventListener("mouseup",mouseupHandler) } function mousemoveHandler(e){ div.style.left=e.clientX-offsetX+"px" div.style.top=e.clientY-offsetY+"px" } function mouseupHandler(e){ document.removeEventListener("mousemove",mousemoveHandler) document.removeEventListener("mouseup",mouseupHandler) } // Simple upgraded version var div = document.querySelector ("div"); var offsetX,offsetY; div.addEventListener("mousedown",mouseHandler); function mouseHandler(e){ if(e.type==="mousedown"){ offsetX=e.offsetX; offsetY=e.offsetY; document.addEventListener("mousemove",mouseHandler) document.addEventListener("mouseup",mouseHandler) }else if(e.type==="mousemove"){ div.style.left=e.clientX-offsetX+"px" div.style.top=e.clientY-offsetY+"px" }else if(e.type==="mouseup"){ document.removeEventListener("mousemove",mouseHandler) document.removeEventListener("mouseup",mouseHandler) } } Note a. The style of the dragged element must be set to absolute or relative position to be effective. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of several error handling when Nginx fails to start
>>: How to implement scheduled backup of MySQL in Linux
Table of contents nonsense text The first router/...
Table of contents 1. Pull the centos image 2. Bui...
First, perform a simple Docker installation. To c...
Nginx log description Through access logs, you ca...
MySQL is a multi-user managed database that can a...
Centos7 uses yum to install MySQL and how to achi...
Table of contents 1. Container service update and...
Without further ado, let me give you the code. Th...
Table of contents Error message Cause Error demon...
As we all know, there are two types of images in c...
question: In some browsers, such as 360 browser...
The nginx.conf configuration file is as follows u...
Preface Today I installed MySQL and found that th...
1. Uninstall the JDK that comes with centeros fir...
vue+el-upload multiple files dynamic upload, for ...