JavaScript to achieve simple drag effect

JavaScript to achieve simple drag effect

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:
  • JQuery Dialog (JS modal window, draggable DIV)
  • Analysis of how to use Sortable.js drag and drop sorting
  • js to achieve drag effect
  • JavaScript implements touch screen drag function on mobile terminal
  • Simple drag effect implemented using js
  • JS realizes beautiful window drag effect (can change size, maximize, minimize, close)
  • Example of file drag and drop upload function implemented by JS
  • Using javascript to implement mouse drag events
  • JS component Bootstrap Table table row drag effect implementation code
  • JS implements the touch screen drag function on mobile devices

<<:  HTML code that can make IE freeze

>>:  Teach you to connect to MySQL database using eclipse

Recommend

Boundary and range description of between in mysql

mysql between boundary range The range of between...

javascript to switch pictures by clicking a button

This article example shares the specific code of ...

How to export and import .sql files under Linux command

This article describes how to export and import ....

How to view the docker run startup parameter command (recommended)

Use runlike to view the docker run startup parame...

Example of implementing colored progress bar animation using CSS3

Brief Tutorial This is a CSS3 color progress bar ...

Learn one minute a day to use Git server to view debug branches and fix them

Debug branch During the normal development of a p...

Example of using CASE WHEN in MySQL sorting

Preface In a previous project, the CASE WHEN sort...

How to find the specified content of a large file in Linux

Think big and small, then redirect. Sometimes Lin...

Remote Desktop Connection between Windows and Linux

When it comes to remote desktop connection to Lin...

15 JavaScript functions worth collecting

Table of contents 1. Reverse the numbers 2. Get t...

A quick solution to the problem of PC and mobile adaptation

When making a web page, we usually need to consid...

Sitemesh tutorial - page decoration technology principles and applications

1. Basic Concepts 1. Sitemesh is a page decoratio...

Steps to export the fields and related attributes of MySQL tables

Need to export the fields and properties of the t...

What is HTML?

History of HTML development: HTML means Hypertext...

A brief discussion on mobile terminal adaptation

Preface The writing of front-end code can never e...