This article shares the specific code of JavaScript to achieve the mouse drag effect for your reference. The specific content is as follows The effect picture this time is as follows: I think the difficulty of this experiment is to keep the relative position of the box and the mouse unchanged, and to achieve the dragging effect by pressing and moving the mouse. How to achieve the drag effect? We need to use three functions: In the callback function of the mouse press , we need to get the initial position of the mouse through In the mouse move callback function, we need to get the current position of the box based on the mouse position and the difference calculated previously, and then change its left and top values. Don't forget to set the position to absolute (because I forgot...) In the callback function of mouse up , we need to clear the mouse movement and mouse up, by setting Also pay attention! ! ! The mouse move function and the mouse lift function should be written in the mouse press function, because we want to design the subsequent behavior after the mouse is pressed, and it is very important that: The mouse down function is div's, the mouse move and mouse up function is document's Because we don't mean to move the mouse in the div, but to move the entire page The key points are probably these. Here is the code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #box{ width: 100px; height: 100px; background-color: aquamarine; border-radius: 14px; box-shadow: 2px 2px 6px rgba(0,0,0,.3); /* Hey guys, you want to move and change the left without setting the position. . . */ position: absolute; } </style> </head> <body> <div id="box"></div> <script> let box = document.getElementById("box"); box.onmousedown=function(event){ let disx = event.clientX-box.offsetLeft; let disy=event.clientY-box.offsetTop; //This is not box.onmousemove, it is document.onmousemove document.onmousemove=function(event){ box.style.left=event.clientX-disx+'px'; box.style.top=event.clientY-disy+'px'; } //To be written in ommousedown document.onmouseup=function(){ //Set both to null document.onmousemove=null; document.onmouseup=null; return false; } } </script> </body> </html> 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:
|
<<: How to uninstall MySQL cleanly (tested and effective)
>>: CSS realizes the layout method of fixed left and adaptive right
Here are 30 best practices for HTML beginners. 1....
1. Installation of the decompressed version (1). ...
Table of contents 1. Basic principles 2. Specific...
Copy code The code is as follows: <head> &l...
Table of contents 1 Install Docker 2 Configuring ...
Swiper is a sliding special effects plug-in built...
Table of contents 1. HttpGET 2. HTTP POST WebSock...
Running environment, Idea2020 version, Tomcat10, ...
<br />Previous article: Web Design Tutorial ...
The component lifecycle is usually where our busi...
The blogger said : I have been writing a series o...
Table of contents Problem Description Principle A...
Table of contents 1. Introduction 2. Usage 3. Dev...
Windows 10 1903 is the latest version of the Wind...
This article introduces how to monitor the ogg pr...