offset Offset is the offset. Using the offset series of related properties, you can dynamically obtain the position (offset), size, etc. of the element, such as: Commonly used attributes of the offset series include: The difference between offset and style
Case 1: Real-time display of mouse coordinatesDemo <!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>Getting the mouse position-01</title> <style> .box { width: 500px; height: 500px; background-color: #55aaff; margin-left: 50px; } </style> </head> <body> <p>Get the mouse coordinates in the box</p> <div class="box"></div> <script> // Click in the box and want to get the distance between the mouse and the box // Implementation: // 1. Get the coordinates of the mouse on the page, e.pageX, e.pageY // 2. Get the distance from the box to the page, box.offsetLeft, box.offsetTop // 3. Subtracting the two will give you the coordinates of the mouse in the box // Let's see the implementation process below! const box = document.querySelector(".box"); box.addEventListener("mousemove", function(e) { // console.log(e.pageX, e.pageY); // console.log(box.offsetLeft, box.offsetTop); const x = e.pageX - this.offsetLeft; const y = e.pageY - this.offsetTop; box.textContent = `x: ${x}, y: ${y}`; }); </script> </body> </html> Case 2: Drag moduleDemo <!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>Module Drag-02</title> <style> * { margin: 0; padding: 0; } .login, .modal { display: none; } .login { width: 512px; height: 280px; position: fixed; border: #ebebeb solid 1px; left: 50%; top: 50%; background-color: #fff; box-shadow: 0 0 20px #ddd; z-index: 999; transform: translate(-50%, -50%); text-align: center; } .modal { position: absolute; top: 0; left: 0; width: 100vw; height: 100vh; background-color: rgba(0, 0, 0, 0.3); z-index: 998; } .login-content { margin: 100px auto; text-align: center; } .login-content h3:hover, .closeBtn:hover { cursor: pointer; } .closeBtn { position: absolute; right: 10px; top: 10px; } .login h4 { margin-top: 10px; } .login h4:hover { cursor: move; } </style> </head> <body> <div class="login-content"> <h3 id="openLogin">Click to pop up the simulation box</h3> </div> <div class="login"> <div class="closeBtn" id="closeBtn">Close</div> <h4 class="loginHeader">Click me to drag</h4> </div> <div class="modal"></div> <script> // Get element const login = document.querySelector(".login"); const modal = document.querySelector(".modal"); const closeBtn = document.querySelector("#closeBtn"); const openLogin = document.querySelector("#openLogin"); // Click the display element openLogin.addEventListener("click", () => { modal.style.display = "block"; login.style.display = "block"; }); closeBtn.addEventListener("click", () => { modal.style.display = "none"; login.style.display = "none"; }); // Implement the drag and drop function // 1. Press the mouse to get the coordinates of the mouse in the box const loginHeader = document.querySelector(".loginHeader"); loginHeader.addEventListener("mousedown", function (e) { const x = e.pageX - login.offsetLeft; const y = e.pageY - login.offsetTop; const move = function (e) { login.style.left = `${e.pageX - x}px`; login.style.top = `${e.pageY - y}px`; }; // 2. Move the mouse document.addEventListener("mousemove", move); document.addEventListener("mouseup", function () { document.removeEventListener("mousemove", move); }); }); </script> </body> </html> This is the end of this article about using JavaScript offset to get mouse coordinates and drag modules within the window. For more information about using JavaScript to get mouse coordinates and drag modules within the window, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to insert 10 million records into a MySQL database table in 88 seconds
>>: Nginx uses reverse proxy to implement load balancing process analysis
<br />Navigation design is one of the main t...
http://www.cppcns.com/shujuku/mysql/283231.html Y...
If prompted to enter a key, select [I don’t have ...
Table of contents 1. What is JSONP 2. JSONP cross...
Let me share with you a creative opening realized...
Table of contents Solution 1: Rebuild Replicas Pr...
One demand Generally speaking, a company has mult...
Some time ago, I encountered the problem that the...
Today I found a problem in HTML. There are many d...
Add rules to the el-form form: Define rules in da...
Table of contents Preface Add sudo write permissi...
The relationship between Tomcat logs A picture is...
Table of contents 1. Introduction 2. Aggregation ...
The nginx configuration is as follows: Such as ht...
<br />What is web2.0? Web2.0 includes those ...