This article shares the specific code of JavaScript to achieve a simple drag effect for your reference. The specific content is as follows 1. Build the scaffolding first: * { margin: 0; padding: 0; } p { background: skyblue; text-align: center; } html, body { width: 100%; height: 100%; } .mask { width: 100%; height: 100%; position: fixed; left: 0; top: 0; background: rgba(0, 0, 0, .5); display: none; } .login { width: 400px; height: 300px; background: purple; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); display: none; cursor: move; } .login>span { display: inline-block; width: 50px; height: 50px; background: red; position: absolute; top: 0; right: 0; } <p>I am a p tag</p> <a href="http://www.baidu.com" >Official website</a> <div class="mask"></div> <div class="login"> <span></span> </div> 2. Logical part //1. Get the element to be operated const oP = document.querySelector("p"); const oMask = document.querySelector(".mask"); const oLogin = document.querySelector(".login"); const oClose = oLogin.querySelector(".login>span"); // console.log(oClose); //2. Listen for click events oP.onclick = function() { oMask.style.display = "block"; oLogin.style.display = "block"; }; oClose.onclick = function() { oMask.style.display = "none"; oLogin.style.display = "none"; }; //3. Listen for the press and move events of the login box oLogin.onmousedown = function(e) { e = e || e.window; //1. Calculate the fixed distance const x = e.pageX - oLogin.offsetLeft; const y = e.pageY - oLogin.offsetTop; // console.log(x); //2. Listen for mobile events oLogin.onmousemove = function(e) { e = e || e.window; //3. Calculate the offset after moving let offsetX = e.pageX - x; let offsetY = e.pageY - y; //4. Reset the position of the login box oLogin.style.left = offsetX + 'px'; oLogin.style.top = offsetY + 'px'; }; }; oLogin.onmouseup = function() { oLogin.onmousemove = null; }; 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:
|
<<: HTML code that can make IE freeze
>>: Teach you to connect to MySQL database using eclipse
1. Reasons If the system is Centos7.3, the Docker...
It has always been very difficult to achieve wave...
The author of this article @子木yoyo posted it on hi...
Table of contents Preface start step Troubleshoot...
An at-rule is a declaration that provides instruc...
1. Why do we need to divide tables and partitions...
FileReader is an important API for front-end file...
For example, to query yesterday's newly regis...
Original code: center.html : <!DOCTYPE html>...
The function I want to achieve is to open a new w...
The Internet is an organism that is constantly ev...
First, download the diagram 1. First uninstall th...
Install TomCat on Windows This article will intro...
<br />Related articles: 9 practical tips for...
Environment Introduction Operating system: centos...