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
The specific code for JavaScript to implement the...
1. Install the built-in Linux subsystem of win10 ...
Preface: I received crazy slow query and request ...
Table of contents The principle of Vue asynchrono...
Today I will introduce a small Javascript animati...
Table of contents Review of Vue2 responsive princ...
Preface: Recently, I encountered a management sys...
1. Python automatically runs at startup Suppose t...
1 Introduction When we write SQL statements to op...
This article shares the MySQL 5.7 installation an...
hint This plug-in can only be accessed under the ...
Unique “About”-Pages A great way to distinguish yo...
Table of contents 1. useState hook 2. useRef hook...
1. Spread Operator The spread operator is three d...
I personally feel that the development framework ...