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
Table of contents 1. Reverse the numbers 2. Get t...
Sometimes we want to execute a command in a conta...
Preface: In project development, some business ta...
Table of contents 1. Environmental Preparation 2....
1. Virtual environment virtualenv installation 1....
This tutorial shares the specific code of MySQL5....
Today, I am sharing the valuable experience of a ...
Operation effect: html <!-- This element is no...
1. Background Generally, for Docker containers th...
Step 1: Ensure that MySQL has binlog enabled show...
Ordered List XML/HTML CodeCopy content to clipboa...
Copy code The code is as follows: <div content...
For example: Copy code The code is as follows: <...
Script requirements: Back up the MySQL database e...
Table of contents 1. Why NanoID is replacing UUID...